cocos2dx精灵点击事件 中 如果一个精灵被击中了,需要消失,用什么函数来实现呢?

Cocos2dx&Shake实现精灵的抖动效果
&最近使用cocos2dx需要用到一个需求,就是关于图片精灵抖动的动作效果。稍微查了一下,找到一个CCShake用来实现这样效果的。不过网上几乎都是在2.x平台上的。所以我根据需求把它改成能用在3.x平台上的。下面放一下代码先:
//& CCShake.h
//& Code by Francois
//& Contact:
//& 何遵祖修改于&支持cocos2dx
3.0 修正了动作执行精灵位置错误问题
测试环境:cocos2d-x-3.0rc1
__war__CCShake__
__war__CCShake__
"cocos2d.h"
namespace cocos2d;
class Shake
: public ActionInterval
& // Create the action with a time and a strength
(same in x and y)
产生震动效果的初始化函数参数,两个方向相同
& & // @param
d 震动持续的时间
& & // @param
strength 震动的幅度
& & static
Shake* create(float d, float strength);
& & // Create
the action with a time and strengths (different in x and
产生震动效果的初始化函数,两个方向值不一样
&&static Shake* create(float d,
float strength_x, float strength_y);
initWithDuration(float d, float strength_x, float
strength_y);
& //以下都是重写父类抽象类的函数(必须重写)
& & virtual
Shake* clone()
& & virtual
Shake* reverse(void)
& & virtual
void startWithTarget(Node *target)
& & virtual
void update(float time)
& & virtual
void stop(void);
protected:
&// Initial position of the shaked node
&// 精灵的位置
&float _initial_x, _initial_y;
&// Strength of the action
& &// 抖动的幅度
&float _strength_x, _strength_y;
//& Shake.cpp
"CCShake.h"
really useful, but I like clean default constructors
Shake::Shake() : _strength_x(0), _strength_y(0),
_initial_x(0), _initial_y(0)
Shake::create(float d, float strength )
& & // call
other construction method with twice the same strength
& & return
create( d, strength, strength );
Shake::create(float duration, float strength_x, float
strength_y)
*p_action = new Shake();
p_action-&initWithDuration(duration, strength_x,
strength_y);
p_action-&autorelease();
& & return
Shake::initWithDuration(float duration, float strength_x, float
strength_y)
(CCActionInterval::initWithDuration(duration))
& &&_strength_x
= strength_x;
_strength_y =
strength_y;
& & return
function. I included it here so that you can compile the whole
returns a random value between min and max included
fgRangeRand( float min, float max )
&float rnd =
((float)rand()/(float)RAND_MAX);
&return rnd*(max-min)+
Shake::update(float time)
&float randx = fgRangeRand( -_strength_x,
_strength_x );
&float randy = fgRangeRand( -_strength_y,
_strength_y );
&// move the target to a shaked
&_target-&setPosition(Vec2(_initial_x +
& &_initial_y +
Shake::clone(void) const
&&auto a = new
&&a-&initWithDuration(_duration,
_strength_x, _strength_y);
&&a-&autorelease();
Shake::reverse() const
&&return Shake::create(_duration,
-_strength_x, -_strength_y);
Shake::startWithTarget(Node *target)
CCActionInterval::startWithTarget(target);
& & // save
the initial position
& & _initial_x
= target-&getPosition().x;
& & _initial_y
= target-&getPosition().y;
Shake::stop(void)
& & // Action
is done, reset clip position
_target-&setPosition(Vec2( _initial_x, _initial_y )
CCActionInterval::stop();
&Cocos2dx3.0中对ActionInterval类中的抽象类方法增加多了两个,一个是clone(),一个是reverse()。前者作用是起到一个复制的作用,后者是反向,让动作以当初设定的相反方向执行。
&这个Shake主要的核心是在update和fgRangeRand方法中,主要思路是在fgRangeRand中在类的_strength(-_strength
~&_strength)值的范围里面产生随机数,然后根据精灵位置加上这里产生的值,从而不断的快速改变位置来参数抖动的效果。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。&&国之画&&&& &&
版权所有 京ICP备号-2
迷上了代码!cocos2dx 精灵的碰撞检测和消灭(3)
HelloWorldSence.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include cocos2d.h
using namespace cocos2d;
class HelloWorld : public cocos2d::CCLayerColor
// Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
virtual bool init();
// there's no 'id' in cpp, so we recommend to return the class instance pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// preprocessor macro for static create() constructor ( node() deprecated )
CREATE_FUNC(HelloWorld);
void addTarget();
void spriteMoveFinished(CCNode *sender);
void gameLogic(cocos2d::CCTime dt);
void ccTouchesEnded(CCSet *touches, CCEvent *event);
CCArray *aarayT//敌人
CCArray *arrayP//飞镖
void update(CCTime dt);
#endif // __HELLOWORLD_SCENE_H__
HelloWorldScence.cpp
#include HelloWorldScene.h
#include SimpleAudioEngine.h
using namespace cocos2d;
using namespace CocosD
CCScene* HelloWorld::scene()
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene-&addChild(layer);
// return the scene
// on init you need to initialize your instance
bool HelloWorld::init()
//////////////////////////////
// 1. super init first
if ( CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)) )
CCSize winSize = CCDirector::sharedDirector()-&getWinSize();//获取屏幕大小
arrayProjectile = CCArray::create();
aarayTarget = CCArray::create();
float sprite_scale = 2.0;
CCSprite *Player =
CCSprite::create(Player.png);
Player-&setScale(sprite_scale);
Player-&setPosition(ccp(Player-&getContentSize().width*sprite_scale/2.0, winSize.height/2.0));
this-&addChild(Player);
aarayTarget-&retain();
arrayProjectile-&retain();
this-&schedule(schedule_selector(HelloWorld::gameLogic), 1.0);
this-&schedule(schedule_selector(HelloWorld::update));
this-&setTouchEnabled(true);
void HelloWorld::gameLogic(cocos2d::CCTime dt){
this-&addTarget();
void HelloWorld::addTarget(){
CCSize winSize = CCDirector::sharedDirector()-&getWinSize();
CCSprite *target = CCSprite::create(Target.png);
//随机位置
int minY = target-&getContentSize().height/2.0;
int maxY = winSize.height - target-&getContentSize().height/2.0;
int rangeY = maxY - minY;
int actualY = rand()%rangeY + minY;
target-&setPosition(ccp(winSize.width - target-&getContentSize().width/2.0, actualY));
target-&setTag(1);
this-&addChild(target);
aarayTarget-&addObject(target);
//随机速度
float minDuration = 2.0;
float maxDuration = 4.0;
int rangeDuration = maxDuration - minD
float actualDuration = rand()%rangeDuration + minD
CCFiniteTimeAction *actionMove = CCMoveTo::create(actualDuration, ccp(0 - target-&getContentSize().width/2.0, actualY));//0代表屏幕外,这句表示在3秒内从初始位置移动到屏幕外
//增加一个回调函数,回收移动到屏幕外的精灵
CCFiniteTimeAction *actionMoveDone = CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished));
target-&runAction(CCSequence::create(actionMove,actionMoveDone,NULL));
void HelloWorld::spriteMoveFinished(cocos2d::CCNode *sender){
CCSprite *sprite = (CCSprite *)
// this-&removeChild(sprite, true);
if (sprite-&getTag() == 1) {
aarayTarget-&removeObject(sprite);//清除敌人
}else if(sprite-&getTag() == 2){
arrayProjectile-&removeObject(sprite);//清除飞镖
//发射飞镖
void HelloWorld::ccTouchesEnded(cocos2d::CCSet *touches, cocos2d::CCEvent *event){
CCTouch *touch = (CCTouch *)touches-&anyObject();
CCPoint location = touch-&getLocationInView();
location = this-&convertTouchToNodeSpace(touch);
CCSize winSize = CCDirector::sharedDirector()-&getWinSize();
CCSprite *projectile = CCSprite::create(Projectile.png);
projectile-&setPosition(ccp(20, winSize.height/2));
float offX = location.x - projectile-&getPositionX();
float offY = location.y - projectile-&getPositionY();
if (offX &= 0) {
projectile-&setTag(2);
this-&addChild(projectile);
arrayProjectile-&addObject(projectile);
float angle = offY/offX;
float realX= winSize.width + projectile-&getContentSize().width/2;
float realY = realX * angle + projectile-&getPositionY();
CCPoint finalPosition = ccp(realX, realY);
//获取飞镖飞行时间
float length = sqrtf(realX *realX + realY*realY);
float velocity = 480;
float realMoveDuration = length/
projectile-&runAction(CCSequence::create(CCMoveTo::create(realMoveDuration, finalPosition),CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished)),NULL));
//碰撞检测,消除飞镖和敌人
void HelloWorld::update(cocos2d::CCTime dt){
for (int i = 0; i & aarayTarget-&count(); i++) {
CCSprite *target = (CCSprite *)aarayTarget-&objectAtIndex(i);
for (int j = 0; j & arrayProjectile-&count(); j++) {
CCSprite *projectile = (CCSprite *)arrayProjectile-&objectAtIndex(j);
if (target-&boundingBox().intersectsRect(projectile-&boundingBox())) {
aarayTarget-&removeObjectAtIndex(i);
arrayProjectile-&removeObjectAtIndex(j);
this-&removeChild(target);
this-&removeChild(projectile);
void HelloWorld::menuCloseCallback(CCObject* pSender)
CCDirector::sharedDirector()-&end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_)
运行结果截图:
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'quick-cocos2dx-lua之精灵移动后的回调函数 - 博客频道 - CSDN.NET
新手学cocos2d-x
QQ交流群:
分类:quick-cocos2dx
请您先把目光集中在下面这个一行代码的移动方法上:
transition.moveTo(node, {x = x, y = y, time = 1})
我之前一直在用上面这种方法,刚刚开始还觉得用的挺好,能将游戏中需要的精灵移动到指定的位置。但是后来接触到分布执行事件后,该方法根本不能达到自己想要的效果,同时代码量增加了很多,显然作为一个程序员来说,这是很难过的事情,后来从大牛那里知道了如下回调方法,顿时觉得写起来叼了很多(如下):
--------------------------------
-- @param node 要移动的对象
-- @param x 横向坐标
-- @param y 纵向坐标
local function fly_func(node, x, y, time)
if node then
local move = {}
move[#move + 1] = cc.MoveTo:create(time, cc.p(x, y))
move[#move + 1] = cc.CallFunc:create(function(event)
--动作执行完到这里
--这里你可以进行任何操作,比如一个骰子掷完后接下来要干的事情可以在这里写。
local sequence = transition.sequence(move)
node:runAction(sequence)
虽然代码多于第一种方法,但是该回调方法可以逆天的一步一步执行接下来的每一件事情,没有任何冲突。
排名:千里之外
(10)(5)(0)(0)

参考资料

 

随机推荐