Cocos2d动作: 创建重复性动作

本文介绍如何使用Cocos2d-x游戏引擎中的动作系统来创建角色重复跳跃的效果。通过创建JumpBy对象并设置跳跃幅度、次数等参数,实现了游戏角色连续跳跃的动作效果。
  1.     /* 创建重复性动作 */
  2.    
  3.     // 创建jumpBy对象
  4.    
  5.    auto jumpBy = JumpBy::create(0.3f, Point(50,1), 100, 1);
  6.    
  7.    // 创建一个永久性的动作,将jump动作添加进去
  8.    // auto repeatForeverAction = RepeatForever::create(jumpBy);
  9.    // guaiwu->runAction(repeatForeverAction);
  10.    // 创建一个指定动作次数的动作,跳十次
  11.    auto repeatAction = Repeat::create(jumpBy,10);
  12.    guaiwu->runAction(repeatAction);
  13.    

检查VScocos2dx4.0代码问题,并修改错误 #include "Thorn.h" #include "HelloWorldScene.h" // StaticThorn StaticThorn* StaticThorn::create(const cocos2d::Size& size, const cocos2d::Vec2& position) { StaticThorn* thorn = new (std::nothrow) StaticThorn(); if (thorn && thorn->initWithFile("Thorn.png")) { thorn->autorelease(); thorn->setPosition(position); thorn->setContentSize(size); thorn->setupPhysics(); return thorn; } CC_SAFE_DELETE(thorn); return nullptr; } void StaticThorn::setupPhysics() { damageBody = cocos2d::PhysicsBody::createBox(this->getContentSize(), cocos2d::PhysicsMaterial(0.0f, 0.0f, 1.0f)); damageBody->setDynamic(false); damageBody->setCategoryBitmask(0x08); damageBody->setCollisionBitmask(0x01); damageBody->setContactTestBitmask(0x01); damageBody->setTag(THORN_DAMAGE_TAG); this->setPhysicsBody(damageBody); triggerBody = cocos2d::PhysicsBody::createBox(this->getContentSize(), cocos2d::PhysicsMaterial(0.0f, 0.0f, 1.0f)); triggerBody->setDynamic(false); triggerBody->setCategoryBitmask(0x10); triggerBody->setCollisionBitmask(0x01); triggerBody->setContactTestBitmask(0x01); triggerBody->setTag(THORN_TRIGGER_TAG); this->addChild(cocos2d::Node::create()); this->getChildByTag(0)->setPhysicsBody(triggerBody); } void StaticThorn::onTrigger() { // Do nothing for static thorn } // MovingThorn MovingThorn* MovingThorn::create(const cocos2d::Size& size, const cocos2d::Vec2& position) { MovingThorn* thorn = new (std::nothrow) MovingThorn(); if (thorn && thorn->initWithFile("Thorn.png")) { thorn->autorelease(); thorn->setPosition(position); thorn->setContentSize(size); thorn->setupPhysics(); return thorn; } CC_SAFE_DELETE(thorn); return nullptr; } void MovingThorn::setupPhysics() { Thorn::setupPhysics(); } void MovingThorn::onTrigger() { auto moveAction = cocos2d::MoveBy::create(1.0f, cocos2d::Vec2(0, 100)); auto removeAction = cocos2d::RemoveSelf::create(); auto sequence = cocos2d::Sequence::create(moveAction, removeAction, nullptr); this->runAction(sequence); } // InvisibleThorn InvisibleThorn* InvisibleThorn::create(const cocos2d::Size& size, const cocos2d::Vec2& position) { InvisibleThorn* thorn = new (std::nothrow) InvisibleThorn(); if (thorn && thorn->initWithFile("Thorn.png")) { thorn->autorelease(); thorn->setPosition(position); thorn->setContentSize(size); thorn->setOpacity(0); thorn->setupPhysics(); return thorn; } CC_SAFE_DELETE(thorn); return nullptr; } void InvisibleThorn::setupPhysics() { Thorn::setupPhysics(); } void InvisibleThorn::onTrigger() { auto fadeIn = cocos2d::FadeIn::create(0.5f); auto moveAction = cocos2d::MoveBy::create(1.0f, cocos2d::Vec2(0, 100)); auto removeAction = cocos2d::RemoveSelf::create(); auto sequence = cocos2d::Sequence::create(fadeIn, moveAction, removeAction, nullptr); this->runAction(sequence); } // StretchingThorn StretchingThorn* StretchingThorn::create(const cocos2d::Size& size, const cocos2d::Vec2& position, float stretchLength) { StretchingThorn* thorn = new (std::nothrow) StretchingThorn(); if (thorn && thorn->initWithFile("Thorn.png")) { thorn->autorelease(); thorn->setPosition(position); thorn->setContentSize(size); thorn->stretchLength = stretchLength; thorn->setupPhysics(); return thorn; } CC_SAFE_DELETE(thorn); return nullptr; } void StretchingThorn::setupPhysics() { Thorn::setupPhysics(); } void StretchingThorn::onTrigger() { auto stretchAction = cocos2d::ScaleBy::create(1.0f, 1.0f, stretchLength / this->getContentSize().height); auto removeAction = cocos2d::RemoveSelf::create(); auto sequence = cocos2d::Sequence::create(stretchAction, removeAction, nullptr); this->runAction(sequence); } #ifndef THORN_H #define THORN_H #include "cocos2d.h" class Thorn : public cocos2d::Sprite { public: static const int THORN_DAMAGE_TAG = 400; static const int THORN_TRIGGER_TAG = 500; virtual void setupPhysics() = 0; // 纯虚函数 virtual void onTrigger() = 0; // 纯虚函数 protected: cocos2d::PhysicsBody* damageBody; cocos2d::PhysicsBody* triggerBody; }; class StaticThorn : public Thorn { public: static StaticThorn* create(const cocos2d::Size& size, const cocos2d::Vec2& position); virtual void setupPhysics() override; virtual void onTrigger() override; }; class MovingThorn : public Thorn { public: static MovingThorn* create(const cocos2d::Size& size, const cocos2d::Vec2& position); virtual void setupPhysics() override; virtual void onTrigger() override; }; class InvisibleThorn : public Thorn { public: static InvisibleThorn* create(const cocos2d::Size& size, const cocos2d::Vec2& position); virtual void setupPhysics() override; virtual void onTrigger() override; }; class StretchingThorn : public Thorn { public: static StretchingThorn* create(const cocos2d::Size& size, const cocos2d::Vec2& position, float stretchLength); virtual void setupPhysics() override; virtual void onTrigger() override; private: float stretchLength; }; #endif // THORN_H
06-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值