Mouse overlapping and hitboxes


(Mike Evmm) #1

Hello,
I feel like I’m about to go insane. I’m trying to make a clickable button (whose scale keeps changing), but for some reason

  1. when setting the hitbox it doesn’t matter what originX/originY is set to (the hitbox stays centered when viewing it from the console) and whether it’s positive or negative
    and
  2. this.collidePoint(this.x, this.y, world.mouseX, world.mouseY) (@Button class) seems to be returning false, no matter what.

    Welp.

(azrafe7) #2
public class Button extends Entity
{
	private var image:Image;
	private var tween:MultiVarTween;
	
	public function Button() 
	{
		super();
		image = Image.createRect(150, 80);
		addGraphic(image);
		image.centerOrigin();
		type = "button";
	}
	
	override public function added():void 
	{
		super.added();
		x = FP.halfWidth;
		y = FP.halfHeight;
		tween = FP.tween(image, { scale:2 }, 3, { complete:tweenScale } );
	}
	
	override public function update():void 
	{
		super.update();
		
		setHitbox(image.scaledWidth, image.scaledHeight, image.scaledWidth / 2, image.scaledHeight / 2);
		
		image.color = collidePoint(x, y, FP.world.mouseX, FP.world.mouseY) ? image.color = 0xFF0000 : image.color = 0x00FF00;
	}
	
	private function tweenScale():void
	{
		tween = FP.tween(image, { scale:2.5 - image.scale }, 3, { complete:tweenScale } );
	}		
}

(Mike Evmm) #3

That’s quite similar to what I currently have.

EDIT: Well, weird. Restarting FD (without making any changes) solved the problem. That’s strange. Thanks @azrafe7 for your help!


(azrafe7) #4

Does that code work for you?

[swf]


(Mike Evmm) #5

Yes perfectly. Also collidePoint seems to be responding normally now.


(azrafe7) #6

Good!