我的船出现在图层上。当按下发射按钮时,船应该移动到计算好的位置。检测到触摸,坐标计算正确,但船不会移动。我已经阅读了这个论坛上关于 runAction 的 500 多个 Q 和 A,但没有任何帮助。我还重新启动了 Xcode 并清理了目标。
ShipManager 的界面如下:
#import <Foundation/Foundation.h>
#import "Common.h"//imports cocos2d.h
@interface ShipManager : NSObject {
GameLayer *layer;
CCSprite *ship;
...
NSArray *shipTargetArray;
CCArray *shipArray;
int podValue;
int podKey;
int topPod;
int botPod;
float x;
float y;
}
@property (nonatomic, retain) CCSprite *ship;
@property (nonatomic, retain) CCSprite *pod;
...
-(id)initWith:(GameLayer *)gameLayer;
-(void)updateShip;
-(void)resetShip;
-(void)touchedExtinguishButton;
@end
部分实现文件:
#import "ShipManager.h"
#import "PodGroupManager.h"
#import "GameLayer.h"
@implementation ShipManager
@synthesize ship;
...
int loadState;
//LAUNCH SHIP
-(void)launchShip {
NSNumber *theTarget;
CGPoint targetPosition;
int shipTarget = absInt(topPod * botPod);
theTarget = [NSNumber numberWithInt:shipTarget];
NSInteger targetIndex = [shipTargetArray indexOfObject:theTarget];
NSLog(@"target = %i; index = %i",shipTarget,targetIndex);//LOG:target = 20; index = 12
self.ship = [shipArray objectAtIndex:0];//NEW CODE.
NSLog(@"ship tag = %i",ship.tag);//LOG:ship tag = 100
NSLog(@"%@",shipArray);//LOG" <CCArray = 055209A0> = ( <CCSprite = 05520A40 | Rect = (2.00,2.00,148.00,200.00) | tag = 100 | atlasIndex = 10>, )
x = ship.position.x;
y = ship.position.y + 10 +34 * targetIndex;
targetPosition = ccp(x,y);
NSLog(@"target x = %f; y = %f",x, y);//LOG: target x = 384.000000; y = 754.000000
//WHAT I WANT THE SHIP TO DO
id action = [CCSequence actions:
[CCMoveTo actionWithDuration:1.0f position:targetPosition],
[CCDelayTime actionWithDuration:1.0f],
[CCMoveTo actionWithDuration:.05f position:SHIP_START_POS],
nil];
[ship runAction:action];//SHIP DOES NOT MOVE*/
//ALSO TRIED: [self.ship runAction:action]; NO MOVEMENT
//ABOVE NOT WORKING -- TRYING SIMPLER MOVE
//CCMoveTo* move = [CCMoveTo actionWithDuration:1.0f position:targetPosition];
//[ship runAction:move]; //TRYING SIMPLER ACTION - STILL NO MOVEMENT
//JUST TRYING TO GET THE SHIP THERE
//ship.position = targetPosition; //NO MOVEMENT EITHER
if (missionState == SHIP_EXT) {
NSLog(@"extinguish fuse");//logs button press correctly
}
}
...
//RESET SHIP TO LAUNCHING PAD
-(void)resetShip {
ship.position = SHIP_START_POS;
[self setShipState:SHIP_EMPTY];
[self setMissionState:SHIP_IDLE];
ship.visible = YES;
}
//SETUP SHIP
-(void) setupShip {
shipArray = [[CCArray alloc]initWithCapacity:1];//just added - latest attempt
ship = [CCSprite spriteWithSpriteFrameName:@"ship.png"];
[layer.batchNode addChild:ship z:SHIP_Z tag:SHIP_TAG];
[shipArray addObject:ship];//just added - latest attempt
[self resetShip];
}
...
//INITIALIZE SHIPMANAGER
-(id)initWith:(GameLayer *)gameLayer {
if ((self = [super init]) ) {
layer = gameLayer;
[self setupShip];
...
[self setTopPod:0];
[self setBotPod:0];
//MAKE SHIPTARGET ARRAY
shipTargetArray = [[NSArray alloc] initWithObjects:
[NSNumber numberWithInt:1],
...
[NSNumber numberWithInt:25],
nil];
}
return self;
}
-(void) dealloc {
[shipTargetArray release];
[shipArray release];
self.ship = nil;
[super dealloc];
}
@end
我知道 launchShip 方法正在执行,因为所有 NSLog 都按预期打印并且结果是正确的。我在 launchShip 中尝试了 3 个不同的选项,我依次评论/取消评论它们以查看是否有任何工作 - 没有。
我是 Objective-c 和 cocos2d 的新手,我怀疑问题是我试图告诉 runAction 运行操作的方式。我确信有一种方法可以正确发送消息,任何有关正确方法的帮助将不胜感激。
提前感谢您的帮助。
在下水之前,将 2 个燃料舱装载到船上。燃料舱具有数值。这些值决定了船舶的目标目的地。所有这些都在起作用。装载燃料舱并计算正确的目的地。