0

I'm using Sprite Kit Xcode 6 and I need to add a sprite and it won't appear when I try to add it... I created a Cocoa Touch Class and The files are below..

level1Layout.m

#import "level1Layout.h"
#import "GameScene.h"

@implementation level1Layout 

SKSpriteNode *testEnemy;

-(void) addSprite{
    NSLog(@"FFFFFFF");
    [self addChild:testEnemy];

}

-(void) testMethod{
    NSLog(@"adad");

    testEnemy = [SKSpriteNode spriteNodeWithImageNamed:@"TestEnemy"];
    testEnemy.position = CGPointMake(512, 384);

    NSLog(@"%f", testEnemy.position.x);
    NSLog(@"%f", testEnemy.position.y);

    [NSTimer scheduledTimerWithTimeInterval:0.1
                                     target:self
                                   selector:@selector(addSprite)
                                   userInfo:nil
                                    repeats:NO];

}

-(void) runAddLevel1Layout{
    [[GameScene new] runLevel1Layout];
}

@end

(I used the NSLogs to make sure it was being called)

level1Layout.h

#import <SpriteKit/SpriteKit.h>
#import <Foundation/Foundation.h>
#import "GameScene.h"

@interface level1Layout : SKScene

-(void) testMethod;
-(void) runAddLevel1Layout;
-(void) addSprite;

@end

And the main class where it starts, if someone wants me to put it in, just let me know.

GameScene.m

#import "GameScene.h"
#import "level1Layout.h"

@implementation GameScene

SKSpriteNode *trans;
SKSpriteNode *level1ButtonRed;

-(void)didMoveToView:(SKView *)view {
    /* Setup your scene here */

    level1ButtonRed = [SKSpriteNode spriteNodeWithImageNamed:@"levelButtonRed"];
}

-(void) runLevel1Layout{
    [[level1Layout new] testMethod];
}

+(void) addLevel1Layout{

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        trans.position = location;

        [NSTimer scheduledTimerWithTimeInterval:2
                                         target:self
                                       selector:@selector(runLevel1Layout)
                                       userInfo:nil
                                        repeats:NO];
    }
}

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
}

@end

Anyone know?

D.C.Adams
  • 15
  • 8
  • you are not adding the sprite as child. Also you shouldn't be using nstimer: http://stackoverflow.com/questions/23978209/spritekit-creating-a-timer/23978854#23978854 – CodeSmile Jan 04 '15 at 16:47
  • Can we see how `testMethod` is being called? i.e. the main class, as you call it. @LearnCocos2D is he not adding the sprite as a child in the `addSprite` method? – Andriko13 Jan 04 '15 at 17:19
  • I tried using what you told me to do, but the methods were never called. @LearnCocos2D – D.C.Adams Jan 04 '15 at 18:30
  • I added 'GameScene.m' into the question, and 'testMethod' is called in there. The 'GameScene.h' file just declares the methods. @Andriko13 – D.C.Adams Jan 04 '15 at 18:30
  • Thanks, what are your NSLog messages? I don't think addSprite is being called with your NSTimer there – Andriko13 Jan 04 '15 at 22:15

0 Answers0