2014-01-17 61 views
0

我有一個Spritekit項目在我的主屏幕上的xcode。不過,我真的想從另一個班級管理它,以便不會聚集我的主人。所以我添加了一個新類來管理和處理它。可悲的是,我似乎無法讓它顯示在我的主要(或者我可能做錯了)。我myscene.m由幾乎相同的代碼,包括飛船,這是我太感動了代碼(我沒有刻意添加方法,如touchesBegan通過iOS精靈從一個類到另一個

// joysitck.m 
// controlpad 
// 


#import "joysitck.h" 


@implementation joysitck 
{ 
    BOOL controlButtonOn; 
} 
float controlx=200; 
float controly=200; 
float touchX = 0.0; 
float touchY=0.0; 


- (instancetype)init 
    { 
     if (self = [super init]) { 
      self.name = @"controlPadNode"; 

     SKSpriteNode *controlPadNode = [SKSpriteNode spriteNodeWithImageNamed:@"game-controller-frontb"]; 

     controlPadNode.position = CGPointMake(controlx,controly); 
     [controlPadNode setScale:0.5]; 
     controlPadNode.name = @"controlPadNode"; 
     controlPadNode.zPosition = 1.0; 

     [self addChild:controlPadNode]; 
    } 
    return self; 
} 



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInNode:self]; 
    SKNode *node = [self nodeAtPoint:location]; 

    //if control pad touched 
    if ([node.name isEqualToString:@"controlPadNode"]) { 
     touchX = location.x; 
     touchY = location.y; 
     controlButtonOn= true; 
    } 
} 

//when touch moves 
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInNode:self]; 
    SKNode *node = [self nodeAtPoint:location]; 

    //if control pad touched 
    if ([node.name isEqualToString:@"controlPadNode"]) { 
     touchX = location.x; 
     touchY = location.y; 
     controlButtonOn= true; 
    } 
} 

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInNode:self]; 
    SKNode *node = [self nodeAtPoint:location]; 

    //if control pad touched 
    if ([node.name isEqualToString:@"controlPadNode"]) { 
     controlButtonOn= false; 
    } 
} 

//update method 
-(void)update:(NSTimeInterval)currentTime { 

    if (controlButtonOn) { 

     //the control pad 
     SKNode *controlNode = [self childNodeWithName:@"controlPadNode"]; 
     SKNode *node = [self childNodeWithName:@"spaceShipNode"];  
     float angle = atan2f (touchY - controly, touchX - controlx) ; 
     SKAction *moveShip=[SKAction moveByX:6*cosf(angle) y:0 duration:0.005]; 
     [node runAction: moveShip]; 

    } 
} 

@end 
+0

搜索節點樹以獲得所需的節點:https://developer.apple.com/library/ios/documentation/ GraphicsAnimation/Conceptual/SpriteKit_PG/Nodes/Nodes.html#// apple_ref/doc/uid/TP40013043-CH3-SW17 – LearnCocos2D

+0

@ LearnCocos2D我添加了下面的代碼,也是我的主要場景,但是控制板沒有顯示。我錯過了什麼? - (SKNode *)controlPadNode return {self childNodeWithName:@「controlPad」]; } –

+0

你的問題是什麼? –

回答

1

你還沒有添加joystick節點到現場。

將此代碼添加到MySceneinitWithSize法([self addship]前):

joysitck *joysitckNode = [[joysitck alloc] init]; 
[self addChild:joysitckNode]; 
+0

仍然看不到操縱桿,btw在你的第二行你的意思是joystickNode對嗎?我是否正確地在joystick.m中添加了操縱桿? –

+0

是的。什麼是「joysitck」類型?可能是「位置」有問題,試着玩不同的值 –

+0

啊是的......它確實顯示,但它是位置。此外,所有的方法,如觸摸開始和更新,它們不應該在joystick.m文件中?遊戲杆顯示,但現在不移動飛船節點。 (以m定義)。我在joystick.m中加入太空船作爲孩子嗎? –

相關問題