在触摸事件上更新CCParticleSystemQuad上的emitter.gravity

时间:2011-12-22 01:00:01

标签: iphone objective-c xcode cocos2d-iphone

我创造了一个简单的cocos2d iPhone游戏,它有一个CCParticleSystemQuad发射器,可以在场景中发出形状像树叶的粒子,给人一种风的幻觉。

现在,风(重力)从右到左吹过现场。我目前正试图弄清楚如何更新emitter.gravity以从(-500,80)切换到(500,80),希望不会删除已经绘制的粒子。

在这个例子中,我希望在屏幕上任何地方发生的触摸事件上进行切换。

我的触摸事件应该是什么样的?

如何检测屏幕上任何位置发生的触摸?

我也从未实现过预定的更新循环。这是我应该思考的方向吗?我想一个更基本的问题是,我是否正确地采用了这种方式?

这是我到目前为止的代码:

我的初学者:

-(id) init
{
if( (self=[super init])) {

    CCSprite * sky = [CCSprite spriteWithFile:@"sky.png"];
    [self addChild:sky z:0 tag:1];

     windDirection = -200;   
    [self leaveEmitters];
    }
return self;
}

我的离开游戏功能

-(void) leaveEmitters{

NSLog(@"The wind is :%i", windDirection);
CCParticleSystemQuad * emitter;
emitter = [[CCParticleSystemQuad alloc] initWithTotalParticles:100];
emitter.texture = [[CCTextureCache sharedTextureCache] addImage: @"particlesLeaves.png"];
emitter.emitterMode = kCCParticleModeGravity;
emitter.duration = -1;
emitter.gravity = ccp(windDirection, -80);
emitter.angle = 0;
emitter.angleVar = 360;
emitter.speed = 10;
emitter.speedVar = 100;
emitter.radialAccelVar = 0;
emitter.tangentialAccel = 0;
emitter.tangentialAccelVar = 0;
emitter.life = 10;
emitter.lifeVar = 0;
emitter.startSpin = 0;
emitter.startSpinVar = 360;
emitter.endSpin = 0;
emitter.endSpinVar = 720;

ccColor4F startColorVar = {255, 100, 0, 0};
ccColor4F startColor = {0, 240,0, 255};   
emitter.startColor = startColor;

emitter.startColorVar = startColorVar;
emitter.endSize = emitter.startSize;



emitter.startSize = 60.0f;
emitter.emissionRate = 3;
emitter.blendAdditive = NO;
emitter.position = ccp(500,250); 
[self addChild: emitter z:10]; 
emitter.autoRemoveOnFinish = YES; 


CCParticleSystemQuad * emitter2;
emitter2 = [[CCParticleSystemQuad alloc] initWithTotalParticles:100];
emitter2.texture = [[CCTextureCache sharedTextureCache] addImage: @"particlesLeaves.png"];
emitter2.emitterMode = kCCParticleModeGravity;
emitter2.duration = -1;
emitter2.gravity = ccp(windDirection, 0);
emitter2.angle = 0;
emitter2.angleVar = 360;
emitter2.speed = 10;
emitter2.speedVar = 100;
emitter2.radialAccelVar = 0;
emitter2.tangentialAccel = 0;
emitter2.tangentialAccelVar = 0;
emitter2.life = 10;
emitter2.lifeVar = 0;
emitter2.startSpin = 0;
emitter2.startSpinVar = 360;
emitter2.endSpin = 0;
emitter2.endSpinVar = 720;
emitter2.startColor = startColor;
emitter2.endSize = emitter.startSize;
emitter2.startSize = 60.0f;
emitter2.emissionRate = 3;
emitter2.blendAdditive = NO;
emitter2.position = ccp(-500,250);  
[self addChild: emitter2 z:10]; 
emitter2.autoRemoveOnFinish = YES; 


}

最后,我的ccTouchesBegan功能完全不起作用。为什么呢?

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch  *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
int x = location.x;
int y= location.y;
CCSprite * sky = (CCSprite *) [self getChildByTag:1];
sky.anchorPoint = ccp(0, 0);
CGRect skyHitBox = CGRectMake(sky.position.x, sky.position.y, 500, 500);

if (CGRectContainsPoint(skyHitBox, location)) {
NSLog(@"touch accepted: x: %i y:%i", x, y);
}
}

任何帮助,反馈或建议的学习方向都将非常感激。非常感谢!

编辑:我在发布此消息后20秒回答了我自己的问题......

如果有人想发布自己的答案,我会再花上7个小时。

1 个答案:

答案 0 :(得分:0)

你可以将CCParticleSystemQuad *发射器保留为私有实例成员,这样你就可以修改ccTouchesBegan中的emitter.gravity

我没有得到你试图达到的风效果。如果你想简单地从一个500,80切换到-500,80,这很容易

emitter.gravity = ccp(-emitter.gravity.x, emitter.gravity.y);

如果你想根据你所击中天空盒的女巫一侧来改变风向,那么击中中心就不会有风,你的触摸距离中心越远,风越强。

我没有尝试过,但应该是一个很好的起点。

首先需要获取天空盒坐标中的触摸点

CGPoint skyTouchedPoint = ccp( x - sky.position.x, y - sky.position.y)

然后您可以将天空框的中心点定义为

CGPoint skyCenterPoint = ccp(sky.size.width/2, sky.size.height/2);

并获得一个指向右侧的矢量,如果您触摸天空盒的右半侧,如果触摸天空盒的左半侧则指向左侧

CGPoint windDirection = ccpSub(skyTouchedPoint, skyCenterPoint);

此向量模块越高,距您触摸的中心越远,您现在应该将此向量模块增加一个适合您的因素

windDirection = ccpMult(windDirection, 10.f); //try different values here

现在你可以只使用这个向量的x分量,如果你想让风总是使用相同的y(你发布的值为80)