6regor Posted May 8, 2014 Share Posted May 8, 2014 Hi, I am new to Phaser. Could anyone please give me tips on how to possibly cause a sprite to pop up after the player sprite collides with another specific sprite + presses a key ? For example, in gameboy pokémon you can walk up to a sign or a person and press (A) to cause text to appear. Sorry about the very untechnical language, I hope you understand what I mean. Thanks,6regor Link to comment Share on other sites More sharing options...
valueerror Posted May 8, 2014 Share Posted May 8, 2014 for a specific code example you can actually use i would need to know if you are working with arcade physics or p2 physics.. in general this should be very easy.. you define a collision callback and in this callback function you create the sprite at whatever position you want it to appear.. in p2 you could do something like this:otherperson.body.onBeginContact.add(mycustomcreatespritefunction,this);function mycustomcreatespritefunction(object1){ if (object1.sprite && object1.sprite.name == 'whatever') { somesprite = game.add.sprite(x, y, 'key'); }} in arcade you could do something like this: game.physics.arcade.collide(player, otherperson,mycustomcreatespritefunction);but there are other ways to do what you want... if you want to trigger something on A:var Akey; // outsideAkey = game.input.keyboard.addKey(Phaser.Keyboard.A); // in create functionif (Akey.isDown){ //in update function //do whatever you need to do} Link to comment Share on other sites More sharing options...
6regor Posted May 9, 2014 Author Share Posted May 9, 2014 Thanks for your help. I'm using arcade physics. Is it possible to create a new sprite only when both; collision between player and othersprite occurs AND Akey.isDown ? Link to comment Share on other sites More sharing options...
kass Posted May 9, 2014 Share Posted May 9, 2014 you could add a if statement in your collision function// if player collides with signSignHandler: function (player, Sign) { // if player clicks A/ActionButton button while colliding with sign if (this.ActionButton.isDown) // || A.isDown { // do stuff here } }of you could add a if statement right in your update function // if player collides with sign// if player clicks A/ActionButton button while colliding with signif(this.game.physics.arcade.overlap(this.player,this.sign) && this.ActionButton.isDown ){ // do stuff here }i hope that helps Link to comment Share on other sites More sharing options...
6regor Posted May 9, 2014 Author Share Posted May 9, 2014 Kass, adding an if statement in a collision function worked. However, only if collision is ongoing (ie. arrow key and ActionButton are simultaneously pressed). Is there a way to achieve the same result if the player is right in front of the sign and ActionButton is pressed? Also, how would you add an additional statement to say that if a different button is pressed after this, the conditions change or ie. the new sprite that was created gets killed/disappears? Appreciate the help. Link to comment Share on other sites More sharing options...
kass Posted May 9, 2014 Share Posted May 9, 2014 hmm i'm using 2.0.3 and the if statement works as long as i'm colliding/touching the sign (i don't have to be simultaneously pressing buttons). are you using ".overlap" or ".collide"? it would help if you could post a piece of the code to give us an example of what's going on. Link to comment Share on other sites More sharing options...
6regor Posted May 9, 2014 Author Share Posted May 9, 2014 In update function:game.physics.arcade.collide(compscrn, player,cmscrr);cmscrr function:function cmscrr(compscrn, player) { if (Akey.isDown) { mssg = game.add.sprite(0, 0, 'message'); }}It only works if i'm actively colliding ie. arrow key is pressed down. After that, how can I add something along the lines of:if (Zkey.isDown){ mssg.kill();}At the moment the above if statement completely freezers the player upon merely colliding with compscrn. Link to comment Share on other sites More sharing options...
kass Posted May 9, 2014 Share Posted May 9, 2014 try changing ".collide" to ".overlap" that way as long as the two object are touching the function will trigger for getting back to back input you could do something similar to this but you might have to tweak it a little for phaser you could have a boolean control your message toggle(i think thats that you want)function cmscrr(compscrn, player) { if (Akey.isDown) { SignMssg = true; mssg = game.add.sprite(0, 0, 'message'); } else if (z.isDown && SignMssg === true) { SignMssg = false; mssg.kill(); }}above code was quickly typed and not tested Link to comment Share on other sites More sharing options...
6regor Posted May 9, 2014 Author Share Posted May 9, 2014 Kass, problem solved for overlap vs. collide. Thank you for that. As for the back to back input boolean control idea, it makes perfect sense to me but for some reason the else if statement is not working; nothing happens. I'm not sure how to tweak it so that it works. Any ideas? Thanks again for your help. Link to comment Share on other sites More sharing options...
valueerror Posted May 9, 2014 Share Posted May 9, 2014 just another thought.. because you are using arcade physics there is yet another approach to this problem: distanceBetween(source, target) so you would write something like this (pseudo):if distanceBetween(player, target) < 20 && keyA.isDown : display spriteelse : hide spriteIMHO the easiest way to do this.. no collision needed and the text will be visible as long as the A key is pressed and the player is not moving out of a well defined range around the target.. Link to comment Share on other sites More sharing options...
Recommended Posts