1

私の船がレイヤーに表示されます。発射ボタンに触れると、船は計算された場所に移動するはずです。タッチは検出され、座標は正しく計算されますが、船は動きません。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

すべての NSLog が期待どおりに出力され、結果が正しいため、launchShip メソッドが実行されていることがわかります。launchShip で試した 3 つの異なるオプションがあり、それらを順番にコメント/コメント解除して、機能するかどうかを確認しますが、どれも機能しません。
私は Objective-c と cocos2d を初めて使用します。この問題は、runAction に何に対してアクションを実行するかを伝えようとしている方法にあると思われます。メッセージを適切に送信する方法があると確信しており、その適切な方法についての助けをいただければ幸いです。
助けてくれてありがとう。

船を進水させる前に、2 つの燃料ポッドが船に積み込まれます。燃料ポッドには数値があります。これらの値によって、船の目的地が決まります。そのすべてが機能しています。燃料ポッドがロードされ、正しい目的地が計算されます。

4

1 に答える 1

0
ship = [shipArray objectAtIndex:0];

この割り当ては保持されません。に変更します

self.ship = [shipArray objectAtIndex:0];

後で dealloc メソッドで行う

self.ship = nil;

それを解放します。他の変数 ( shipTargetArray shipArray) も同じように扱います。

于 2012-05-28T21:54:39.800 に答える