我的船出现在图层上。按下发射按钮时,船舶应移动到计算出的位置。检测到触摸,正确计算坐标,但船不会移动。我已经在该论坛上阅读了500多个关于runAction的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对操作进行操作的方式。我敢肯定,有一种方法可以正确发送消息,并且对使用该正确方法的任何帮助将不胜感激。
在此先感谢您的帮助。

在下船之前,将两个燃料舱装载到船上。燃油舱具有数值。这些值确定了船只的目标目的地。所有这些都在起作用。装入燃油箱并计算出正确的目的地。

最佳答案

ship = [shipArray objectAtIndex:0];


此分配不会保留。更改为

self.ship = [shipArray objectAtIndex:0];


稍后在dealloc方法中

self.ship = nil;


释放它。将其他变量(shipTargetArray shipArray)视为相同。

10-06 01:10