到目前为止都很美好!
但是却有一点奇怪,因为炮塔一下子跳转到指定位置去射击,并不是平滑的跟随触摸去转动到指定位置.你可以修复这个问题,但是这需要略微一点的重构(refactoring).
首先打开HelloWorldLayer.h,在你的类中添加下面一个实例变量:
CCSprite *_nextProjectile;
然后回到HelloWorldLayer.m中按如下代码修改cctouchesEnded方法:
- (voID)cctouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if (_nextProjectile != nil) return; // Choose one of the touches to work with UItouch *touch = [touches anyObject]; CGPoint location = [self converttouchToNodeSpace:touch]; // Set up initial location of projectile CGSize winSize = [[CCDirector sharedDirector] winSize]; _nextProjectile = [[CCSprite spriteWithfile:@"projectile2.png"] retain]; _nextProjectile.position = ccp(20,winSize.height/2); // Determine offset of location to projectile CGPoint offset = ccpsub(location,_nextProjectile.position); // Bail out if you are shooting down or backwards if (offset.x <= 0) return; // Determine where you wish to shoot the projectile to int realX = winSize.wIDth + (_nextProjectile.contentSize.wIDth/2); float ratio = (float) offset.y / (float) offset.x; int realY = (realX * ratio) + _nextProjectile.position.y; CGPoint realDest = ccp(realX,realY); // Determine the length of how far you're shooting int offRealX = realX - _nextProjectile.position.x; int offRealY = realY - _nextProjectile.position.y; float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY)); float ve@R_403_6207@ty = 480/1; // 480pixels/1sec float realMoveDuration = length/ve@R_403_6207@ty; // Determine angle to face float angleradians = atanf((float)offRealY / (float)offRealX); float angledegrees = CC_radians_TO_degrees(angleradians); float cocosAngle = -1 * angledegrees; float rotatedegreesPerSecond = 180 / 0.5; // Would take 0.5 seconds to rotate 180 degrees,or half a circle float degreesDiff = _player.rotation - cocosAngle; float rotateDuration = fabs(degreesDiff / rotatedegreesPerSecond); [_player runAction: [CCSequence actions: [CCRotateto actionWithDuration:rotateDuration angle:cocosAngle],[CCCallBlock actionWithBlock:^{ // OK to add Now - rotation is finished! [self addChild:_nextProjectile]; [_projectiles addobject:_nextProjectile]; // Release [_nextProjectile release]; _nextProjectile = nil; }],nil]]; // Move projectile to actual endpoint [_nextProjectile runAction: [CCSequence actions: [CCMoveto actionWithDuration:realMoveDuration position:realDest],[CCCallBlockN actionWithBlock:^(CCNode *node) { [_projectiles removeObject:node]; [node removeFromParentAndCleanup:YES]; }],nil]]; _nextProjectile.tag = 2; [[SimpleAudioEngine sharedEngine] playEffect:@"pew-pew-lei.caf"];}
看起来代码不少,但是实际你并没有修改太多,大多数都仅仅是很小的重构,以下是代码做出的改变:
在方法开始处如果_nextProjectile不为nil则直接退出,意思是你正在处理射击 *** 作 之前你使用的是一个名为projectile的局部变量,然后立即把它添加到场景中.在新的版本中你创建了一个实例变量_nextProjectile,但是没有立即添加到场景中. 你定义了炮塔旋转的速度为180 / 0.5,意味着半秒钟旋转180度. 去计算旋转特定的角度需要多长时间 然后开始一个顺序action去旋转炮塔到正确的角度,然后调用一个block去将导d添加到场景中.偶鸟,试一试吧!编译运行项目,然后现在炮塔应该旋转的平滑多了 ;)
原文地址:Rotating Turrets: How To Make A Simple iPhone Game with Cocos2D 2.X Part 2
总结以上是内存溢出为你收集整理的Cocos2D旋转炮塔到指定角度(三)全部内容,希望文章能够帮你解决Cocos2D旋转炮塔到指定角度(三)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)