【问题标题】:How to make a sprite jump like a frog in cocos2d-x ios game using c++如何使用c++在cocos2d-x ios游戏中让精灵像青蛙一样跳跃
【发布时间】:2014-02-04 09:43:49
【问题描述】:

我正在尝试将精灵制作为青蛙,它将从屏幕顶部出现并在 y 轴 =0 处向下移动到底部。它作为普通 CCMoveTo 工作正常,但我希望在跳跃后青蛙应该休息 1 秒钟,然后再跳。移动有某种延迟。谁能告诉我这个。我也附上我的代码。 我的青蛙动画是从 fly1.png 到 fly5.png。我只想在每次移动后有一个延迟,或者我们可以说我只想在每次延迟 1 秒后调用 CCMove,直到青蛙到达 y 轴 = 0 任何帮助将不胜感激。谢谢

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d;
using namespace CocosDenshion;

CCScene* HelloWorld::scene()
{

CCScene *scene = CCScene::create();

HelloWorld *layer = HelloWorld::create();

scene->addChild(layer);

return scene;
}


bool HelloWorld::init()
{

if ( !CCLayer::init() )
{
    return false;
}
CCSize winSize=CCDirector::sharedDirector()->getWinSize();

_bgNode = CCNode::create();
_bgNode->setPosition(ccp(winSize.width/2, winSize.height/2));
this->addChild(_bgNode, -1);

_bgSprite = CCSprite::create("bg_2.jpg");
_bgNode->addChild(_bgSprite);

float rX = winSize.width/_bgSprite->getContentSize().width;
float rY = winSize.height/_bgSprite->getContentSize().height;

_bgNode->setScaleX(rX);
_bgNode->setScaleY(rY);

z=CCSprite::create("fly1.png");
z->setScaleX(rX);
z->setScaleY(rY);
z->setPosition(ccp(winSize.width/2,winSize.height+1));
this->addChild(z);
CCAction *a=CCRepeatForever::create(HelloWorld::getAnimationWithFrames(1,5));
z->runAction(a);

z->runAction(CCSequence::create(CCMoveTo::create(2.0, ccp(winSize.width/2, 0)), CCCallFuncN::create(this, callfuncN_selector(HelloWorld::setInvisible)), NULL));

return true;
}

cocos2d::CCAnimate* HelloWorld::getAnimationWithFrames(int from, int to)
{
CCArray* frames = CCArray::create();


for (int i = from; i <= to; i++)
{
    CCString *str = CCString::createWithFormat("fly2%d.png", i);
    CCSpriteFrame *f = CCSpriteFrame::create(str->getCString(), CCRect(0,0,256,400));
    frames->addObject(f);
}
//(frames,speedofmovementofanimation);
CCAnimation *animation = CCAnimation::createWithSpriteFrames(frames,0.15f);

CCAnimate *a = CCAnimate::create(animation);
return a;
}

void HelloWorld::setInvisible()
{
this->removeChild(z,true);
}

【问题讨论】:

  • CCJumpTo 和 CCJumpBy 方法用于跳跃精灵而不是 CCMoveTo。
  • 我想让青蛙在每次跳跃后休息一秒钟。你能帮我吗?
  • id 延迟 = [CCDelayTime actionWithDuration:1.0];那是客观 c,但你在 C++ 中使用相同的方法。
  • 先生,这是主要问题..延迟的地方..看看我的 CCMoveto 线...
  • z->runAction(CCSequence::create(CCMoveTo::create(2.0, ccp(winSize.width/2, 0)), CCCallFuncN::create(this, callfuncN_selector(HelloWorld::setInvisible) )), NULL));

标签: c++ ios xcode4 cocos2d-x


【解决方案1】:
        Firstly, you have to create a frogJump() function. Then,in frogJump() function add the following code:

        void HelloWorld::frogJump()

        {

        CCJumpTo* jumpTo = CCJumpTo::create(1,ccp(x/6,y/8),y/1.2f,1);

        z->runAction(jumpTo);

        }

        Then, in the init() function add the following line:


       this->scheduleOnce(SEL_SCHEDULE(&HelloWorld::frogJump),0);
  this->schedule(SEL_SCHEDULE(&HelloWorld::frogJump),2);

【讨论】:

  • 兄弟,实际上当它到达 y 轴 =0 时我也必须执行一些操作,这就是我在移动操作中使用 cccalfunc 的原因
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多