gpt4 book ai didi

objective-c - runAction 没有移动我的船

转载 作者:行者123 更新时间:2023-11-28 17:32:37 24 4
gpt4 key购买 nike

我的船出现在图层上。当按下发射按钮时,船应该移动到计算好的位置。检测到触摸,正确计算坐标,但船不会移动。我已经阅读了该论坛上关于 runAction 的 500 多个问答,但没有任何帮助。我还重新启动了 Xcode 并清理了目标。

这是 ShipManager 的接口(interface):

#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 个燃料舱装载到船上。燃料舱具有数值。这些值决定了船舶的目标目的地。所有这些都在起作用。装载燃料舱并计算出正确的目的地。

最佳答案

ship = [shipArray objectAtIndex:0];

此分配将不会保留。改成

self.ship = [shipArray objectAtIndex:0];

稍后在dealloc方法中做

self.ship = nil;

释放它。对待其他变量 (shipTargetArray shipArray) 相同。

关于objective-c - runAction 没有移动我的船,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10788961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com