我有一個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
搜索節點樹以獲得所需的節點:https://developer.apple.com/library/ios/documentation/ GraphicsAnimation/Conceptual/SpriteKit_PG/Nodes/Nodes.html#// apple_ref/doc/uid/TP40013043-CH3-SW17 – LearnCocos2D
@ LearnCocos2D我添加了下面的代碼,也是我的主要場景,但是控制板沒有顯示。我錯過了什麼? - (SKNode *)controlPadNode return {self childNodeWithName:@「controlPad」]; } –
你的問題是什麼? –