您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-html5之MoveTo&MoveBy

2015-05-10 17:39 302 查看
本文测试所用Cocos2d-html5版本:2.2.1。

MoveTo:移动到某个位置,是绝对距离。

MoveBy:移动一段距离,是相对距离。

使用cc.MoveTo.create(duration, deltaPosition)和cc.MoveBy.create(duration, deltaPosition)来创建动作。

duration
运动周期,单位为s。
deltaPosition
坐标点,使用cc.p(x, y)或者new cc.Point(x, y)

请看下列代码:

var GameScene = cc.Scene.extend({

enemy1: null, // 敌人1

enemy2: null, // 敌人2

enemy3:null, // 敌人3

layer: null, // 布景

winSize: null, // 游戏运行窗口尺寸

onEnter: function () {

this._super();

this.initData();

},

initData: function () {

// 获取尺寸

this.winSize = cc.Director.getInstance().getWinSize();

// 添加布景

this.layer = cc.LayerColor.create(cc.c4(200, 200, 200, 255), this.winSize.width, this.winSize.height);

this.addChild(this.layer);

// 创建动作

var actionTo = cc.MoveTo.create(2, cc.p(this.winSize.width - 40, this.winSize.height - 40));

var actionBy = cc.MoveBy.create(2, cc.p(40, 40));

var actionByBack = actionBy.reverse();

// 添加敌人1

this.enemy1 = cc.Sprite.create(s_enemy_1);

this.layer.addChild(this.enemy1);

this.enemy1.runAction(actionTo);

// 添加敌人2

this.enemy2 = cc.Sprite.create(s_enemy_2);

this.enemy2.setPosition(cc.p(40, 40));

this.layer.addChild(this.enemy2);

this.enemy2.runAction(cc.Sequence.create(actionBy, actionByBack));

// 添加敌人3

this.enemy3 = cc.Sprite.create(s_enemy_3);

this.enemy3.setPosition(cc.p(80, 80));

this.layer.addChild(this.enemy3);

this.enemy3.runAction(cc.MoveBy.create(2, cc.p(100, 0)));

}

});

以下是运行结果截图:

#cyg { font: 14px "微软雅黑"; line-height: 1.6em; } #cyg h2 { margin: 1.33em 0 1em 0; font-size: 16px; } #cyg h3 { margin: 1.33em 0 1em 0; font-size: 14px; } #cyg p { margin: 1em 0; } #cyg_code { overflow-y: hidden; margin: 1em 0; padding-left: 40px; border: 2px solid rgb(200, 200, 200); background: rgb(231, 229, 220); font: 12px courier,arial,sans-serif; list-style: decimal; } #cyg_code li { min-height: 20px; line-height: 20px; padding: 0 1em; border-left: 3px solid rgb(108, 226, 108); background: rgb(240, 240, 240); white-space: pre; } #cyg_code li:nth-child(even) { background: rgb(230, 230, 230); } #cyg_code em { font-style: normal; color: rgb(117, 113, 94); } #cyg_code strong { font-weight: normal; color: rgb(153, 129, 255); } #cyg_code b { font-weight: normal; color: rgb(124, 174, 24); } #cyg_code cite { font-style: normal; color: rgb(198, 93, 8); } #cyg_code span { color: rgb(249, 38, 76); } #cyg_code ins { text-decoration: none; color: rgb(19, 153, 179); }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: