rahulng Posted February 19, 2014 Share Posted February 19, 2014 Hey guys.I wanted to make a fuctionality through which If i select a sprite its gets replaced by a highlighted version of that sprite(the ones with selected in their name) and the other objects become rplaced by their unhighlighted version however with the current code whats happening is when testing the program it seems to be making an stran instance of the unhighlighted versions. function create () { mothership = game.add.sprite(90,100,"mothership"); mothership.inputEnabled=true; mothership.events.onInputDown.add(listener,this); bots=game.add.group();//the bots group bot1=bots.create(400,100,'bot1'); bot1.inputEnabled=true; bot1.events.onInputDown.add(listener,this); bot2=bots.create(700,100,'bot2'); bot2.inputEnabled=true; bot2.events.onInputDown.add(listener,this); } function listener(sprite, pointer) { if(sprite==mothership) { sprite.kill(); MothershipSelect(); Bot1(); Bot2(); } if(sprite==mothershipSelected) { sprite.kill(); Mothership(); } if(sprite==bot1) { sprite.kill(); Bot1Select(); Mothership(); Bot2(); } if(sprite==bot1Selected) { sprite.kill(); Bot1(); } if(sprite==bot2) { sprite.kill(); Bot2Select(); Mothership(); Bot1(); } if(sprite==bot2Selected) { sprite.kill(); Bot2(); } } function MothershipSelect() { mothership.kill(); mothershipSelected = game.add.sprite(80,100,"mothershipSelected"); mothershipSelected.inputEnabled=true; mothershipSelected.events.onInputDown.add(listener,this); } function Mothership() { mothershipSelected.kill(); mothership = game.add.sprite(90,100,"mothership"); mothership.inputEnabled=true; mothership.events.onInputDown.add(listener,this) } function Bot1() { bot1Selected.kill() bot1=bots.create(400,100,'bot1'); bot1.inputEnabled=true; bot1.events.onInputDown.add(listener,this); } function Bot1Select() { bot1.kill(); bot1Selected=bots.create(400,100,'bot1Selected'); bot1Selected.inputEnabled=true; bot1Selected.events.onInputDown.add(listener,this); } function Bot2() { bot2Selected.kill(); bot2=bots.create(700,100,'bot2'); bot2.inputEnabled=true; bot2.events.onInputDown.add(listener,this); } function Bot2Select() { bot2.kill(); bot2Selected=bots.create(700,100,'bot2Selected'); bot2Selected.inputEnabled=true; bot2Selected.events.onInputDown.add(listener,this); } Link to comment Share on other sites More sharing options...
XekeDeath Posted February 19, 2014 Share Posted February 19, 2014 bots.createandgame.add.sprite create new sprite objects every time you call them.So, what you are doing when you click something is creating a new instance of a sprite.What you are not doing is destroying the old ones.The kill function does not destroy the instance, it just swaps the visible and exists bools to false.If you want to completely destroy the sprite and recreate it every time you select something (which is kinda overkill, really, because of the need to reattach the event handlers every time..), swap kill() for destroy(). Alternatively, create a selected and not selected sprite for each object, and use kill()/revive(). For yet another option, consider a sprite sheet with 2 frames, and swap between them. Link to comment Share on other sites More sharing options...
Recommended Posts