Jump to content

How do I check if my player is below a group spike trap?


potatopoo
 Share

Recommended Posts

Hi guys!

 

I recently uncovered the mysteries of groups. http://www.html5gamedevs.com/topic/10774-how-to-add-coins-that-have-specific-positions-via-groups/ Well.. enough for my needs at least.

 

Now i need a way to trigger any one of my falling spike traps with my player. Please have in mind that my traps have specific positions and nothing is randomised. If i used distance, its any direction, right?, so it would trigger my trap if i was walking on the ground above it (2d jazz jackrabbit sidescroller). Same with circle triggers. Ideally, the trigger would be a bit wider so the spike has time to fall if the player continues walking at his current velocity..

 

Any code examples would be greatly appreciated. Please no 1 word explanations, i have no idea what im doing :P

Link to comment
Share on other sites

I'd have an invisible 'trigger' sprite under the trap and do something like:

function create() {  var triggerGroup = game.add.group();  var trap = game.add.sprite(100, 100, 'trap');  // create a trigger with a null key (so it's invisible) and add it to the triggerGroup  var trigger = game.add.sprite(100, 200, null, 0, triggerGroup);  game.physics.arcade.enable([trap, trigger]);  // store a reference to the trap this trigger sets off  trigger.trap = trap;  // set the size of the body appropriately  trigger.body.setSize(100, 100);}function update() {  // check the player against all the triggers, looking for an overlap  game.physics.arcade.overlap(player, triggerGroup, function(player, trigger) {    // ensure a trigger only fires ones    if (!trigger.triggered) {      trigger.triggered = true;      // call a function which causes a specific trap to fall; implementation is up to you      dropTrap(trigger.trap);    }  });}

Obviously this will need fleshing out but that'd form the basis of how to do it. A much better way would be to extend Sprite and create your own Trap object which creates its own trigger and automatically adds it to the correct group for checking, but I'll leave you to figure this out as an exercise :)

Link to comment
Share on other sites

I'm glad i got a response from you :P You are a celebrity around here. Your posts helped me lots with my game.

 

Your code works very well, but i seem to have two problems. Firstly, my game has perma gravity, so when I spawn my trap, it falls down. A way I recently found to battle this is

this.trap.body.moves = false;

Now, I first had an idea to use my coin code to make the game spawn a spike (via invisible trigger i learned from you) behind a dummy stalactite sprite, which would then fall because of gravity, and .kill() itself if it touched the player or the .json ground, but that failed terribly.

 

Secondly, I make the game in a different way than the phaser examples, by making lots of .js files and using this. a lot. Well, i have no clear idea what this. is, but it makes my game magically work when i use it. Sometimes var works and then i dont have to use .this (i recon thats because its a local variable and works only between its brackets). The thing is, when i come to the overlap part of your code, it stops working. I have a theory that its because i havent announced that this.trigger.triggered = false; (but than again triggered is inside trigger... so who knows which rules apply.. makes my brain explode :'( ).

 

And how would one make multiple traps? Like this?:

function create() {this.triggerGroup = this.add.group();this.trap1 = this.add.sprite(100, 100, 'trap');this.trap2 = this.add.sprite(1500, 1200, 'trap');this.trap3 = this.add.sprite(1000, 600, 'trap');this.trigger1 = this.add.sprite(100, 200, null, 0, triggerGroup);this.trigger2 = this.add.sprite(1500, 1350, null, 0, triggerGroup);this.trigger3 = this.add.sprite(1000, 800, null, 0, triggerGroup);this.physics.arcade.enable([trap1, trap2, trap3, trigger1, trigger2, trigger3]);this.trigger1.trap = trap1;this.trigger2.trap = trap2;this.trigger3.trap = trap3;this.trigger1.body.setSize(100, 10);this.trigger2.body.setSize(10, 90);this.trigger3.body.setSize(70, 50);}

And then furthermore.. would i need a seperate colission check for each one? Oh wait.. probably not... but i would need to make a .triggered that is false. No, then it would always be false. I guess that part needs to go into the create() part.. for each trap...

 

Oh no, but then there is a function calling a trigger.trap.. would i need to do an if check for each trap. Inside it i would put a this.trap1.body.moves = true. Now i confused myself again..

 

If you help me, I love you long time :P

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...