Jump to content

One sprite clicks, the other does not.


Gyldstrand
 Share

Recommended Posts

var MapObjects = {};

function setupMapObjects() {
    MapObjects.chest = new PIXI.Sprite(frame("images/chest.png", 0, 0, 50, 50));
    MapObjects.chest.x = 200;
    MapObjects.chest.y = 200;
    MapObjects.chest.anchor.x = 0.5;
    MapObjects.chest.anchor.y = 0.7;
    MapObjects.chest.interactive = true;
    MapObjects.chest.on('click', openChest);
    MapObjects.chest.hitArea = new PIXI.Circle(MapObjects.chest.x, MapObjects.chest.y, 10);
    MapObjectsArray.push(MapObjects.chest);
    stage.addChild(MapObjects.chest);    
};

function openChest() {
    console.log('open');
};


var Player = {};

function setupPlayer(){
    Player.sprite = new PIXI.Sprite(frame("images/Player.png", 0, 0, 128, 128));
    Player.sprite.anchor.x = 0.5;
    Player.sprite.anchor.y = 0.7;
    Player.sprite.x = 150;
    Player.sprite.y = 150;
    Player.vx = 0;
    Player.vy = 0;
    Player.direction;
    Player.directionIdle;
    Player.idle;
    Player.moving;
    Player.frameCycle = 0;
    Player.action = false;
    Player.run = false;
    Player.melee = false;
    Player.sprite.interactive = true;
    Player.sprite.on('click', openChest);
    stage.addChild(Player.sprite);
};

 

MapObjects.chest does not respond to the .on('click', openChest) but Player.sprite does. I don't know man.... I just don't know. I marked this off my To Do list a few days ago cause it was working but now... (head explodes) 

 

I guess I should ask if there is another way to apply the click event to the sprite.

Link to comment
Share on other sites

Chest is under player, right? There was a commit in 3.0.8 or 3.0.9 that made clicking on two objects at the same time impossible. I was against it. For now you can take old version of InteractionManager.processInteractive, that'll solve the problem. Dump it in JS script and execute before you create the renderer:

	//and that's old 3.0.6 precessInteractive
	PIXI.interaction.InteractionManager.prototype.processInteractive = function (point, displayObject, func, hitTest, interactive )
	{
	    if(!displayObject.visible)
	    {
	        return false;
	    }
	
	    var children = displayObject.children;
	
	    var hit = false;
	
	    // if the object is interactive we must hit test all its children..
	    interactive = interactive || displayObject.interactive;
	
	    if(displayObject.interactiveChildren)
	    {
	
	        for (var i = children.length-1; i >= 0; i--)
	        {
	            if(! hit  && hitTest)
	            {
	                hit = this.processInteractive(point, children[i], func, true, interactive );
	            }
	            else
	            {
	                // now we know we can miss it all!
	                this.processInteractive(point, children[i], func, true, false );
	            }
	        }
	
	    }
	
	    if(interactive)
	    {
	        if(hitTest)
	        {
	            if(displayObject.hitArea)
	            {
	                // lets use the hit object first!
	                displayObject.worldTransform.applyInverse(point,  this._tempPoint);
	                hit = displayObject.hitArea.contains( this._tempPoint.x, this._tempPoint.y );
	            }
	            else if(displayObject.containsPoint)
	            {
	                hit = displayObject.containsPoint(point);
	            }
	        }
	
	        if(displayObject.interactive)
	        {
	            func(displayObject, hit);
	        }
	    }
	
	    return hit;
	};

 

Link to comment
Share on other sites

because hitArea overrides containsPoint : 

if(displayObject.hitArea)
{
	// lets use the hit object first!
	displayObject.worldTransform.applyInverse(point,  this._tempPoint);
	hit = displayObject.hitArea.contains( this._tempPoint.x, this._tempPoint.y );
}
else if(displayObject.containsPoint)
{
	hit = displayObject.containsPoint(point);
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...