Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8833193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:43:18+00:00 2026-06-14T08:43:18+00:00

I’m using http://craftyjs.com/ for the Github Game Off, and I’m having some trouble with

  • 0

I’m using http://craftyjs.com/ for the Github Game Off, and I’m having some trouble with animation. The first time I start the animation (when I initialize the player entity in the main scene) it works. But when I set the animation via my CustomControls component, it only plays the first frame of the animation.

I believe the problem is in the CustomControls component, so here’s the code for that:
https://gist.github.com/3992392

Here’s all the code if you want to clone it and test: https://github.com/RylandAlmanza/dragons-suck

If anyone knows what the problem might be, let me know. Thanks!

EDIT: Here’s a minimal jsfiddle example with everything stripped out except for movement and what should be animation: http://jsfiddle.net/x7FDF/

var SPRITE_SIZE = 32;
Crafty.init();
Crafty.canvas.init();
Crafty.sprite(SPRITE_SIZE, "http://i.imgur.com/9sN9V.png", {
    player_east_1: [0, 0],
    player_east_2: [1, 0],
    player_east_3: [2, 0],
    player_west_1: [0, 1],
    player_west_2: [1, 1],
    player_west_3: [2, 1],
    player_south_1: [0, 2],
    player_south_2: [1, 2],
    player_south_3: [2, 2],
    player_north_1: [0, 3],
    player_north_2: [1, 3],
    player_north_3: [2, 3]
});

Crafty.scene("loading", function() {
    Crafty.background("#000");
    Crafty.e("2D, DOM, Text")
    .attr({
        w: 100,
        h: 20,
        x: 150,
        y: 120
    })
    .text("Loading")
    .css({"text-align": "center"});
    Crafty.load(["http://i.imgur.com/9sN9V.png"], function() {
        Crafty.scene("main");
    });
});

Crafty.scene("main", function() {
    Crafty.background("#000");

    var player = Crafty.e("2D, DOM, SpriteAnimation, player_east_1, Collision, TileCollision, CustomControls")
    .attr({
        x: 0,
        y: 0,
        x_velocity: 0,
        y_velocity: 0,
        w: SPRITE_SIZE,
        h: SPRITE_SIZE
    })
    .animate("stand_east", 0, 0, 0)
    .animate("stand_west", 0, 1, 0)
    .animate("stand_south", 0, 2, 0)
    .animate("stand_north", 0, 3, 0)
    .animate("walk_east", [[0, 0], [1, 0], [0, 0], [2, 0]])
    .animate("walk_west", [[0, 1], [1, 1], [0, 1], [2, 1]])
    .animate("walk_south", [[0, 2], [1, 2], [0, 2], [2, 2]])
    .animate("walk_north", [[0, 3], [1, 3], [0, 3], [2, 3]])
    .animate("stand_east", 45, -1)
    .CustomControls();
});


Crafty.c("CustomControls", {
    CustomControls: function() {
        this.bind("EnterFrame", function() {
            var up = Crafty.keydown[Crafty.keys.UP_ARROW];
            var down = Crafty.keydown[Crafty.keys.DOWN_ARROW];
            var left = Crafty.keydown[Crafty.keys.LEFT_ARROW];
            var right = Crafty.keydown[Crafty.keys.RIGHT_ARROW];
            if (up) {
                this.y_velocity = -2;
                if (!this.isPlaying("walk_north")) {
                    this.stop().animate("walk_north", 45, -1);
                }
            }
            if (down) {
                this.y_velocity = 2;
                if (!this.isPlaying("walk_south")) {
                    this.stop().animate("walk_south", 45, -1);
                }
            }
            if (left) {
                this.x_velocity = -2;
                if (!this.isPlaying("walk_west")) {
                    this.stop().animate("walk_west", 45, -1);
                }
            }
            if (right) {
                this.x_velocity = 2;
                if (!this.isPlaying("walk_east")) {
                    this.stop().animate("walk_east", 45, -1);
                }
            }
            if (!left && !right) {
                this.x_velocity = 0;
                this.stop();
            }
            if (!up && !down) {
                this.y_velocity = 0;
                this.stop();
            }
            this.x += this.x_velocity;
            this.y += this.y_velocity;
        });
    }
});

Crafty.scene("loading");​
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:43:19+00:00Added an answer on June 14, 2026 at 8:43 am

    In the end of "enterFrame" you have:

    if (!left && !right) {
        this.x_velocity = 0;
        this.stop();
    }
    if (!up && !down) {
        this.y_velocity = 0;
        this.stop();
    }
    

    Therefore, the animation is always stopped and then restarted from the beginning in the next frame. For example, when you’re player is moving down, (!left && !right) evaluates true and the animations is stopped.

    One solution is to stop the animation only if all 4 directions are false.

    if (!left && !right) {
        this.x_velocity = 0;
    }
    if (!up && !down) {
        this.y_velocity = 0;
    }
    if (!(up | down | left | right)) {
        this.stop();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm making a simple page using Google Maps API 3. My first. One marker
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.