[Solved] MoveTo action works on web but not on phone

So what I want to do is this:

  1. Load a sprite on the right side of the screen
  2. Run a MoveTo action on the sprite to move it from right to left

The bug is the MoveTo action is not working/running on the phone but works when I run my project on web.

Here is the code for generating the obstacles and the MoveTo action (Look at the LoadAnObstacle function):

/* 
 * This file is to setup the obstacles in the race (PlayScene)
 */

var ObstacleLayer = cc.Layer.extend({
    // Declare variables
    space: null,
    topWallHeight: 0,
    bottomWallHeight: 0,
    obstaclesSpritesheet: null,
    screenSize: 0,
    screenOrigin: 0,
    obstacles: [],

    ctor: function (space, topWallHeight, bottomWallHeight) {
        this._super();
        this.topWallHeight = topWallHeight;
        this.bottomWallHeight = bottomWallHeight;
        this.space = space;
        this.init(); // init needs to be called right after assignment of space
    },

    init: function () {
        this._super();

        // Initialize
        this.screenSize = cc.director.getVisibleSize();
        this.screenOrigin = cc.director.getVisibleOrigin();

        // Initialize obstaclesSpritesheet
        cc.spriteFrameCache.addSpriteFrames(res.obstacles_plist);
        this.obstaclesSpritesheet = new cc.SpriteBatchNode(res.obstacles_png);
        this.addChild(this.obstaclesSpritesheet);

        // Start timer for when the next obstacle should load
        this.nextObstacleTimer();

        // Start scheduleUpdate
        this.scheduleUpdate();
    },

    // Timer for when the next obstacle should load
    nextObstacleTimer: function () {
        this.unschedule(this.loadAnObstacle);
        this.schedule(this.loadAnObstacle, Math.floor((Math.random() * 2) + 0.5)); // Generate a random number between 0.5-2.5 (seconds)
    },

    // Load a random obstacle at a random height on the road
    loadAnObstacle: function () {
        // Re-call timer so countdown starts for next obstacle to load
        this.nextObstacleTimer();

        var sprite = new cc.PhysicsSprite.create("#obstacle1.png");
        // Get obstacle size
        var obstacleSize = sprite.getContentSize();

        // Select a random height on the road
        var totalWallHeight = this.topWallHeight - this.bottomWallHeight - obstacleSize.height;
        var obstacleHeight = (Math.random() * totalWallHeight) + this.bottomWallHeight + (obstacleSize.height / 2);

        // Initialize the physics
        var obstacleBody = new cp.Body(1, cp.momentForBox(1, obstacleSize.width, obstacleSize.height));
        obstacleBody.p = cc.p(this.screenOrigin.x + this.screenSize.width, obstacleHeight);
        sprite.setBody(obstacleBody);

        var shape = new cp.BoxShape(obstacleBody, obstacleSize.width, obstacleSize.height);
        shape.setElasticity(elasticity); // These are set in globals.js
        shape.setFriction(friction);
        shape.setCollisionType(2);
        shape.setSensor(true);
        this.space.addShape(shape);

        // Start moving the obstacle
        var obstacleAction = cc.MoveTo.create(1, cc.p(this.screenOrigin.x, obstacleHeight)); //MoveTo(time/seconds, position)
        sprite.runAction(obstacleAction);

        // Add this obstacle sprite to spritesheet
        this.obstaclesSpritesheet.addChild(sprite);
    },

    // Update each frame
    update: function (dt) {
        //Function to check if obstacle reached the end of the screen
    }
});

So the obstacleAction is not working as it’s supposed to on my android device. Am I setting it up wrong? Should it be in the scheduler update?

This is what’s happening on web:

This is what’s happening on my phone:

it’s an issue relating to moving a physics sprite the best practices are to use forces to move physics objects not actions.

1 Like

Of course! MoveTo does not work on a physics sprite.

So to move the physics sprite horizontally from right to left, no change in the y-axis, the best practice is to use forces. Good to know, thanks :slight_smile: That’s the second time you save me.
What about velocity or impulse? Should I just not consider them?

UPDATE:
setVelocity doesn’t seem to work in JS and for my game I feel applyImpulse works better than applyForce. When Force is applied, it takes some time for the physics sprite to get up to speed, using impulse moves the sprite instantly.
Also, this only works if the physics sprite body is added to the space, if it’s not then force and impulse do not work.

1 Like