Cocos2D-JS-Quick-Tutorials icon indicating copy to clipboard operation
Cocos2D-JS-Quick-Tutorials copied to clipboard

06 | Cocos2D-JS | How to Move a Sprite on the Screen

Open Gurigraphics opened this issue 7 years ago • 0 comments

06 | How to Move a Sprite on the Screen

Game.layers.js

In the Game.layers.js a) set the time of movement b) set the horizontal distance of the movement c) set the vertical distance of the movement d) set the motion action

    Game.layers[1].extend = cc.Layer.extend({
        init: function () {      
            this._super(); 
            var game = this;
            Game.layers[1].start( game );  
        } 
    });    
     
    Game.layers[1].start = function( game ){
     
        var size = cc.director.getWinSize();
        var sprite = new cc.Sprite ( "HelloWorld.png" ) ; 
       
        sprite.attr({  
            x: size.width / 2, 
            y: size.height / 2,
            anchorX: 0.5,
            anchorY: 0.5, 
            time: 2,  //a
            moveX: 100, //b
            moveY: 150 //c
        });
          
        game.addChild(sprite);
    
        sprite.runAction( new cc.MoveBy( sprite.time, cc.p( sprite.moveX, sprite.moveY ) ));   //d
         
    }; 

Gurigraphics avatar Apr 29 '18 22:04 Gurigraphics