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); }
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); }
相关文章推荐
- Cocos2d-html5之SkewTo&SkewBy
- Cocos2d-html5之RotateTo&RotateBy
- cocos2d-x学习笔记(7)MoveTo和MoveBy
- Cocos2d-html5之ScaleTo&ScaleBy
- cocos2d anchor与moveBy、moveTo
- cocos2d-x中Moveby和Moveto的解释
- cocos2d-x v3.9 与MoveBy和MoveTo之间的对话
- cocos2d-x实例学习(2)之CCMoveTo&CCMoveBy
- COCOS2D学习日记之 moveto 与 moveby
- Cocos2d-html5之TintTo&TintBy
- cocos2d-x & javascript跨平台初体验
- Cocos2d-x3.0模版容器详解之一:cocos2d::Vector<T>
- Cocos2d-x-html5之HelloWorld深入分析与调试
- 在mac环境下创建cocos2d-x工程出现"permission Denied"的解决方案
- Dynatable – 基于 HTML5 & jQuery 的交互表格插件
- cocos2d-x中 child->m_pParent == 0 异常的可能情况之一
- Cocos2d-x-html5之HelloWorld深入分析与调试
- [Cocos2d-html5] 使用Ant一步一步压缩混淆Cocos2d-html5游戏
- cocos2d-html5 draw 函数
- cocos2d-x跨android&ios平台开发入门教程