Clickable createRect


(Nate ) #1

Hey guys, I am trying to make a shape using Image.createRect and have it be clickable and I feel like I am missing out on something critical. Here is my shape class, the clicking works but only at 0,0 not at the location of the shape that I am spawning.

Here is the Shape.as

package entities 
{
	import net.flashpunk.FP;
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.graphics.Text;
	import net.flashpunk.utils.Input;
	
	public class theShape extends Entity
	{
		private var shape:Image;
		
		public function theShape(posX:int, posY:int, color:int) 
		{
			shape = Image.createRect(32, 32, color);
			setHitbox(32, 32);
			x = posX;
			y = posY;
			graphic = shape;
			layer = 0;
		}
		
		override public function update():void
		{
			if (this.collidePoint(shape.x, shape.y, world.mouseX, world.mouseY))
			{
				if (Input.mouseReleased)click();
			}
			
			super.update();
		}
		
		public function click():void
		{
			trace("clicking shape");
		}
		
		
	}

}

Thanks guys!


(Nate ) #2

I changed the collidePoint from shape.x and shape.y to this.x and this.y and it now works! SOLVED


(Bora Kasap) #3

i can’t see any problems in your code, it looks like should work. i’m using this and it works well.

override added:     this.setHitbox(32, 32);
override update:    if (this.collidePoint(this.x, this.y, Input.mouseX, Input.mouseY))

also that should be faster because of mouse click property is a single number or boolean value but collision check is more than this… so, i think it’s better you don’t check for collision if mouse not clicked yet.

if (Input.mouseReleased)
			{
				if (this.collidePoint(shape.x, shape.y, world.mouseX, world.mouseY)) click();
			}