Easier way to find out if mouse is over an object ?
Shri

Gio,

 

I am working on something like a matching game where selected scene objects are pushed onto a list.

The player would mouse down on the first object to start the list, and then drag the mouse over other objects to add them as well.

I currently do this in the mouseMove handler.

The call to self.select() just adds the scene object to the selected list if it is a new scene object.

this.onMouseMove = function(eventData) {	if (wade.isMouseDown()) {	     var sToW = wade.screenPositionToWorld(self.GAME_LAYER,eventData.screenPosition);	     var oArr = wade.getObjectsInArea({minX: sToW.x-5, minY: sToW.y-5, maxX: sToW.x+5, maxY: sToW.y+5},self.GAME_LAYER);	     if (oArr.length > 0 && oArr[0].isJelly) {		self.select(oArr[0]);	     }	}	// end mouse is down				return true;}	// end onMouseMove

It all currently  works fine.

But I have a couple of questions:

1) is there an easier way to figure out if the mouse is over a scene object, either in the event data, or some other call I don't know about ?

2) what is a good tolerance for the bounding box in the getObjectsInArea call ?

3) if there is not already a call, would it be possible to add something in the future for the mouse / touch similar to the sceneObject.getOverlappingObjects() function ?

 

As always, any help is appreciated !

 

cheers,

Shri

All 3 Comments
Gio

Hi Shri

 

I'd just mouseIn (and potentially mouseOut) events to do that. I know it doesn't work perfectly with moving objects (I don't know exactly why, but I haven't had a chance to investigate properly), but if your objects are static it sure does work.

myObject.onMouseIn = function(){   self.select(this);};

Number 3 is a good idea, I've made a note :)

Shri

Gio,

 

Thanks for the tip, works like a charm.

One other minor note:

When you add an array of behaviors to a scene object.

var jelly = new SceneObject(base,[PhysicsObject,Jelly], xPos, -600);

It is not obvious, from the documentation, that you have to do the following in the behavior file

Jelly = function() {	     this.name = 'Jelly';     .....}; // end Jelly

So that you can do something like this in your code:

obj.getBehavior('Jelly').doSomething();

anyway,

 

ciao!

Shri

Gio

Good point Shri, noted.

 

Thanks

Post a reply
Add Attachment
Submit Reply
Login to Reply