ae如何让箭头移动多个Sprite同时移动

FLASH使用startDrag是同时拖动多个显示对象_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
FLASH使用startDrag是同时拖动多个显示对象
你可能喜欢1171人阅读
在前一章中我们介绍了如何添加一个cocos2d-x精灵中,为游戏场景添加了一个精灵。但一个英雄或许太过孤单,我们应该加入一些敌人,让他来打败。本文我们将讲述如何移动一个cocos2d-x精灵。
void addTarget()函数将会帮我们完成这一工作,敌人将会以随机的速度,从游戏场景左移动到右。
在HelloWorldScence.h里声明void addTarget(),并在HelloWorldScene.cpp里添加以下的代码,(请不要忘记在HelloWorldScene.cpp的开头加入using namespace cocos2d)
1// cpp with cocos2d-x
2void HelloWorld::addTarget()
4 CCSprite *target = CCSprite::spriteWithFile(&Target.png&,
5 CCRectMake(0,0,27,40) );
7 // Determine where to spawn the target along the Y axis
8 CCSize winSize = CCDirector::sharedDirector()-&getWinSize();
9 int minY = target-&getContentSize().height/2;
10 int maxY = winSize.height
11 - target-&getContentSize().height/2;
12 int rangeY = maxY - minY;
13 // srand( TimGetTicks() );
14 int actualY = ( rand() % rangeY ) + minY;
16 // Create the target slightly off-screen along the right edge,
17 // and along a random position along the Y axis as calculated
18 target-&setPosition(
19 ccp(winSize.width + (target-&getContentSize().width/2),
20 actualY) );
21 this-&addChild(target);
23 // Determine speed of the target
24 int minDuration = (int)2.0;
25 int maxDuration = (int)4.0;
26 int rangeDuration = maxDuration - minD
27 // srand( TimGetTicks() );
28 int actualDuration = ( rand() % rangeDuration )
29 + minD
31 // Create the actions
32 CCFiniteTimeAction* actionMove =
33 CCMoveTo::actionWithDuration( (ccTime)actualDuration,
34 ccp(0 - target-&getContentSize().width/2, actualY) );
35 CCFiniteTimeAction* actionMoveDone =
36 CCCallFuncN::actionWithTarget( this,
37 callfuncN_selector(HelloWorld::spriteMoveFinished));
38 target-&runAction( CCSequence::actions(actionMove,
39 actionMoveDone, NULL) );
1// objc with cocos2d-iphone
2-(void)addTarget
4 CCSprite *target = [CCSprite spriteWithFile:@&Target.png&
5 rect:CGRectMake(0, 0, 27, 40)];
7 // Determine where to spawn the target along the Y axis
8 CGSize winSize = [[CCDirector sharedDirector] winSize];
9 int minY = target.contentSize.height/2;
10 int maxY = winSize.height - target.contentSize.height/2;
11 int rangeY = maxY - minY;
13 int actualY = (arc4random() % rangeY) + minY;
15 // Create the target slightly off-screen along the right edge,
16 // and along a random position along the Y axis as calculated
17 target.position =
18 ccp(winSize.width + (target.contentSize.width/2),
19 actualY);
20 [self addChild:target];
22 // Determine speed of the target
23 int minDuration = 2.0;
24 int maxDuration = 4.0;
25 int rangeDuration = maxDuration - minD
27 int actualDuration = (arc4random() % rangeDuration)
28 + minD
30 // Create the actions
31 id actionMove =
32 [CCMoveTo actionWithDuration:actualDuration
33 position:ccp(-target.contentSize.width/2, actualY)];
34 id actionMoveDone =
35 [CCCallFuncN actionWithTarget:self
36 selector:@selector(spriteMoveFinished:)];
37 [target runAction:[CCSequence actions:actionMove,
38 actionMoveDone, nil]];
这里用callfuncN_selector(HelloWorld::spriteMoveFinished)回调了spriteMoveFinished方法,我们需要在HelloWorldScene.h里声明并如下来定义它,
1// cpp with cocos2d-x
2void HelloWorld::spriteMoveFinished(CCNode* sender)
4 CCSprite *sprite = (CCSprite *)
5 this-&removeChild(sprite, true);
1// objc with cocos2d-iphone
2-(void)spriteMoveFinished:(id)sender
4 CCSprite *sprite = (CCSprite *)
5 [self removeChild:sprite cleanup:YES];
1. 关于随机函数。srand和rand是C标准库函数。对于每一个平台来说,你可以先获取毫秒级时间来得到一个随机数。在沃Phone上,这个函数是TimGetTickes(),而在iPhone上,你可以直接通过arc4random()函数来获得随机数。
2. Objc中的YES和NO,在cpp中变为true和false。
3. 回调函数,在objc中用selector:@selector(spriteMoveFinished),但在cpp中实现就比较复杂了,你可以参考cocos2dx\include\selector_protocol.h里的声明。一共有5种回调函数类型
 schedule_selector
 callfunc_selector
 callfuncN_selector
 callfuncND_selector
 menu_selector
如何使用它们,根据所用函数的定义来决定。比如使用CCTimer::initWithTarget函数,它的第二个参数是SEL_SCHEDULE类型,到selector_protocol.h里查一下,可以看到对应的是schedule_selector(_SELECTOR)宏,所以调用时就需要在类里头实现一个void MyClass::MyCallbackFuncName(ccTime)函数,然后把schedule_selector(MyClass::MyCallbackFuncName)作为CCTimer::initWithTarget的第二个参数传入。
之后,我们应该定时地为游戏加入敌人,把以下代码加入到init()函数的返回值前。
1// cpp with cocos2d-x
2// Call game logic about every second
3this-&schedule( schedule_selector(HelloWorld::gameLogic), 1.0 );
1// objc with cocos2d-iphone
2// Call game logic about every second
3[self schedule:@selector(gameLogic:) interval:1.0];
然后在HelloWorldScence.cpp里实现gameLogic()。请注意gameLogic()应该声明为pubilc,否则是无法回调的。
1// cpp with cocos2d-x
2void HelloWorld::gameLogic(ccTime dt)
4 this-&addTarget();
1// objc with cocos2d-iphone
2-(void)gameLogic:(ccTime)dt
4 [self addTarget];
好了,所有事情都做完了,编译并运行,好好享用你的成果。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:128791次
积分:1618
积分:1618
排名:千里之外
原创:24篇
转载:65篇
评论:11条
(1)(1)(1)(1)(2)(2)(2)(3)(4)(2)(9)(1)(2)(4)(8)(6)(7)(5)(1)(1)(3)(1)(6)(9)(3)(2)(2)【图片】请教,多Sprite组合BOSS的移动问题。【construct2吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:2,179贴子:
请教,多Sprite组合BOSS的移动问题。收藏
请问,对于使用【多个Sprite组合成的BOSS】的移动,有没有简单的解决方案?我尝试结合rex_moveto,rex_pin2imagepoint,rex_rotate,rex_spline等Behaviors写移动事件,但是好繁琐啊,还容易出错。请教各位前辈,谢谢。
百度拥有超过1000款测试真机,提供一站式App测试服务,横扫各类App测试需求.帮你告别App测试困扰,支持手游测试,金融测试等各行业APP测试
你可以选择用骨骼动画啊,不然就只能做好每个boss的状态动作了
如果只是坦克的炮塔與底座,或是魂斗羅的上半身跟下半身,還可以用container + pin to image point來面對。可是從你的圖看來你還是用骨骼動畫系統比較好。而且你用骨骼動畫應該就不用pin to image point了,你用spriter就該使用spriter插件將人物素材匯入,這樣才能播放spriter裡面設計的人物動畫。
等一下。。你這個手長腳長的角色該不會是想做成鉸鏈般的感覺吧?
登录百度帐号推荐应用「一个超级新的新手提问」请问怎样让角色移动时有走路的?【gamemaker吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:9,693贴子:
「一个超级新的新手提问」请问怎样让角色移动时有走路的?收藏
昨天刚刚接触到gamemaker,看了下视频教程,角色移动都是一个图片在移动,那怎样可以让角色有走路动作求教
又走路的动作,标题打漏了
8分钟都一个人也没有新手发帖这么不受欢迎吗
键盘事件,sprite_index=走路sprite的名字。我也是新手,不知对不对,你试试
楼上有点瞎说了,连中文代码都出来我也是醉了。楼主你想做走路那你先得画出走路的图然后再改变事件
哦对了楼主,像你这种连基础教程都懒得看懒得学的人,别说不受欢迎了,连理都不会理你,贴吧全都是小学生和新人,大神们很少,可以说是几乎不会在贴吧活动。请先自己摸索,教程说明书摆在那里都是吃fen的吗,不要动不动就来贴吧问没有头绪的问题,没人会回答你的
唔 最近刚好做了一个射击和行走的例子我的思路比较简单啊四个方向 不同的sprite详见:方向键控制 空格射击 大概就是这样 因为是之前的小例子我也是新人 所以请谅解和提出问题
@0,sz@,ta@iphone_2_4.1_3_&bd_page_type=1&baiduid=FD8D7DEF5DC43235EBEE0F8&tj=www_zhidao_normal_7_0_10_title给个教程
登录百度帐号推荐应用

我要回帖

更多关于 移动流量转让 的文章

 

随机推荐