runAction没有移动我的船

时间:2012-05-28 18:27:00

标签: objective-c ios cocos2d-iphone

我的船出现在图层上。触摸启动按钮时,船舶应移动到计算位置。检测到触摸,正确计算坐标,但船舶不会移动。我已经在这个关于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个燃料箱装在船上。燃料箱具有数值。这些值确定了船舶目标目的地。所有这些都有效。加载燃料箱并计算正确的目的地。

1 个答案:

答案 0 :(得分:0)

ship = [shipArray objectAtIndex:0];

此作业不会保留。将其更改为

self.ship = [shipArray objectAtIndex:0];

稍后在dealloc方法中执行

self.ship = nil;

发布它。将其他变量(shipTargetArray shipArray)视为相同。