Jump to content

Animation not playing (after being called by an onDown event) and AI


Luis Felipe
 Share

Recommended Posts

Hey everyone,

 

So I'm in the final stages of my second bi-weekly game. I started with character animations, all animations play correctly when called in the update() function, but when I put the action animation inside a function called in by an onDown event, it doesn't play. Here's what I have:

create: function() {(...)actionKey.onDown.add(this.heroHits, this);}

then:

heroHits: function() {        hero.animations.play('attack');        if(actionTimer == 1 && this.game.physics.overlap(hero, enemy)) {            this.enemyHealth -= 1;            enemyLabel.content = this.enemyHealth;            enemy.body.velocity.x = 1500;            enemy.body.velocity.y = -350;            actionTimer = 0;                    }    },

If you need to look at the entire thing for context, here goes: http://luisfelipeart.com/html5games/002/

 

Triggering the animation in the update() function works as expected (like the run animation), but I don't want to play it continuously. 

 

Any insight on this matter and maybe tips on improving AI would be greatly appreciated! I'm sure it's a very silly mistake, excuse my n00bness :P

 

Thanks

Link to comment
Share on other sites

I don't think those events work on keys, but I may be mistaken. The way it's done in the examples is to poll the key in your update code, just like you're doing with your cursors key objects.

 

If you don't want to play it continuously, change your call to the animation creation to this:

var attack = hero.animations.add('attack', [8,9], 30, false);attack.onComplete.add(function(){ hero.animations.start('idle');}, this);

That will only play the animation once and then switch the animation back to whatever you want. I like your running animation, by the way  :)

 

You started the game challenge, too? That's awesome :) I started it from inspiration at lessmilk.com as well and I'm loving it so far.

Link to comment
Share on other sites

Thanks for the reply!

 

I tried implementing your code, I didn't succeed tonight but I'm sure the problem is on my side, tomorrow I'll hopefully nail it down, this was super useful and informative! :)

 

 

Thanks, the inspiration for this one comes directly from Nidhogg, I'm trying to diversify and learn to make different types of games and art. Haha yes, Lessmilk is definitely an amazing source of inspiration! Looking forward to seeing your games, really enjoyed Octoball :)

Link to comment
Share on other sites

:D Nidhogg is pretty awesome :) I'm also curious to see the end of your game, so I'll definitely bookmark it and check it out later :) I really liked the sprite design for your first game and how you gave a tutorial about it.

 

Thanks for checking out my blog and Octoball. I love hearing what people think of it :)

 

If you have questions about how to implement what I said, we can figure it out together. 

Link to comment
Share on other sites

  • 3 months later...

Hello, I know this is a few months old but I just wanted to ask if anyone had a resolution to this issue?

 

I have an animation that plays when the player runs left or right but when I bind spacebar and try to make the new animation play on it it just sits on the first frame.... its like the arrow keys somehow bypass restarting the animation but the spacebar restarts the animation every time the game updates so it doesn't work...

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