将NSObject添加到SKScene

时间:2014-04-25 06:29:48

标签: ios objective-c sprite-kit

我最近使用SpriteKit制作赛车游戏。我创建了一个名为“GameObject”的NSObject类来存储所有属性,比如物理。我还创建了另一个名为“GameWorld”的NSObject类来存储所有游戏对象,比如创建玩家对象和其他对象及其位置和方向按钮的功能。但是,当我开始编写游戏场景类时,我无法将世界对象添加到场景中,这意味着汽车不会出现在场景中。有人可以帮我解决这个问题吗?我的SKScene课程代码如下:

@interface GamePlay : SKScene
@property (nonatomic) GameWorld *world;
-(void)update:(NSTimeInterval)currentTime;
-(id) initWithSize:(CGSize)s andWorld:(GameWorld *)w;
@end

@implementation GamePlay

- (id) initWithSize:(CGSize)s andWorld:(GameWorld *)w
{
    self = [super initWithSize:s];
    if (self)
    {
        _world = w;
    }
    return self;
}

-(void) didMoveToView:(SKView *)view
{
    if (!self.contentCreated ) {
        [self createSceneContents];
        self.contentCreated = YES;
    }
}

-(void) createSceneContents
{
    // turn off gravity for the world
    self.physicsWorld.gravity = CGVectorMake(0.0f, 0.0f);
    self.scaleMode = SKSceneScaleModeAspectFit;

    //Create the background
    SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"road.png"];
    background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    [self addChild:background];

    //Create the buttons for directions
    SKSpriteNode *up = [SKSpriteNode spriteNodeWithImageNamed:@"Up.png"];
    up.position = CGPointMake(290, 115);
    up.name = @"upDirection";
    [self addChild:up];

    SKSpriteNode *down = [SKSpriteNode spriteNodeWithImageNamed:@"Down.png"];
    down.position = CGPointMake(290, 40);
    down.name = @"downDirection";
    [self addChild:down];

    SKSpriteNode *left = [SKSpriteNode spriteNodeWithImageNamed:@"Left.png"];
    left.position = CGPointMake(30, 40);
    left.name = @"leftDirection";
    [self addChild:left];

    SKSpriteNode *right = [SKSpriteNode spriteNodeWithImageNamed:@"Right.png"];
    right.position = CGPointMake(CGRectGetMidX(self.frame), 40);
    right.name = @"rightDirection";
    [self addChild:right];

    // add objects from GameWorld to this scene using the world's worldNode property
    // Here is the place I confuse. the world doesn't show up 
    [self addChild:_world.worldNode];
}

//making buttons for the cars
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];

    //setting a node to keep the position of each direction buttons
    SKNode *n = [self nodeAtPoint:positionInScene];

    NSLog(@"Node n:%@", n);
    //compare the location of each SKSpriteNode and the touch location
    SKNode *up = [self childNodeWithName:@"upDirection"];
    if (n == up) {
        [self.world goForward];
    }

    SKNode *down =[self childNodeWithName:@"downDirection"];
    if (n == down) {
        [self.world goBackward];
    }

    SKNode *left = [self childNodeWithName:@"leftDirection"];
    if (n == left) {
        [self.world goLeft];
    }

    SKNode *right = [self childNodeWithName:@"rightDirection"];
    if (n == right) {
        [self.world goRight];
    }
}

-(void)update:(NSTimeInterval)currentTime
{
    //I don't know how to start yet. = =
}

@end

GameWorld课程如下:

@interface GameWorld : NSObject
@property (nonatomic) SKNode *worldNode;
@property (nonatomic) NSArray *gameObjs;
-(id)init;
-(void) initializeWorld;
-(void) goForward;
-(void) goBackward;
-(void) goLeft;
-(void) goRight;
@end

@implementation GameWorld

-(id)init
{
    if (self) {
        self = [super init];
        GameObject *player = [[GameObject alloc] initWithImageNamed:@"pCar.png" andPosition:CGPointMake(200, 85)];
        GameObject *otherCar = [[GameObject alloc]initWithImageNamed:@"AICar.png" andPosition:CGPointMake(200, 100)];

        _worldNode = [SKNode node];
        // now create the objects, put them in an SKNode
        // create the _worldNode with a size equal to the virtual world size
        // then add the game objects to that node
        _worldNode.scene.size = CGSizeMake(320, 480);
        _gameObjs = @[player, otherCar];
    }
    return self;
}

- (void) initializeWorld
{
    // add skSpriteNodes to the worldNode
    SKSpriteNode *player = [[SKSpriteNode alloc] initWithImageNamed:@"pCar.png"];
    [_worldNode addChild:player];

    SKSpriteNode *otherCar = [[SKSpriteNode alloc] initWithImageNamed:@"AICar.png"];
    [_worldNode addChild:otherCar];
}

-(void) goForward
{
    GameObject *thePlayer = _gameObjs[1];
    thePlayer.node.position = CGPointMake(thePlayer.node.position.x, thePlayer.node.position.y + 10);
}

-(void) goBackward
{
    GameObject *thePlayer = _gameObjs[1];
    thePlayer.node.position = CGPointMake(thePlayer.node.position.x, thePlayer.node.position.y - 10);
}

-(void) goLeft
{
    GameObject *thePlayer = _gameObjs[1];
    thePlayer.node.position = CGPointMake(thePlayer.node.position.x - 10, thePlayer.node.position.y);
}

-(void) goRight
{
    GameObject *thePlayer = _gameObjs[1];
    thePlayer.node.position = CGPointMake(thePlayer.node.position.x + 10, thePlayer.node.position.y);
}

@end

1 个答案:

答案 0 :(得分:0)

大多数情况下无需使用NSObject。请改用SKNodeSKSpriteNode。通过对这些类进行子类化,您可以将自定义属性添加到它们中。

我建议您使用以下项目结构:

-- GameScene (SKScene)
---- BackgroundLayer (SKNode)
-------- BackgroundNode (SKSpriteNode)
---- UiLayer (SKNode)
-------- your buttons here... (SKSpriteNode)
---- CarLayer (SKNode)
-------- PlayerNode (SKSpriteNode)
-------- OtherCarNode (SKSpriteNode)

在这种情况下,您不必将对象存储在手动创建的数组中,例如gameObjs。例如,可以从此属性访问所有按钮:uiLayer.children

我创建了一个模板,我用于我的Sprite Kit游戏,看看它:

https://github.com/andrew8712/spritekit-game-template