Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

NSInteger values = 0 when touchesBegan is called

Chris1979Chris1979 Posts: 81Registered Users @
I´m passing two NSInteger values from one class, level.m to another class, GameScene.m.

Everything is working fine until the method TouchesBegan is called from GameScene.m, then the values, self.test1 and self.test2 go back to 0 again (or any other value that I decide to set in initWithSize).

I.e. the passed values are being overwritten by the values in initWithSize when TouchesBegan is called.

-(void)BlankPositionAtColumn:(NSInteger) blankY row:(NSInteger)blankX{
NSLog(@"BlankPositionAtColumn: BlankX, BlankY: %d, %d", blankX, blankY);
self.gamescene = [[GameScene alloc] init];
[self.gamescene BlankPosition:blankY row:blankX];

@import SpriteKit;

@class Level;
@class Swap;

@interface GameScene : SKScene
@property (strong, nonatomic) Level *level;
//If the scene recognizes that the user made a swipe, it will call this swipe handler block
@property (copy, nonatomic) void (^swipeHandler)(Swap *swap);

- (void)addSpritesForCookies:(NSSet *)cookies;
- (void)animateSwap: (Swap *)swap completion:(dispatch_block_t)completion;
- (void)animateMatchedCookies:(NSSet *)chains completion:(dispatch_block_t)completion;
- (void)animateFallingCookies:(NSArray *)columns completion:(dispatch_block_t)completion;
- (void)animateNewCookies:(NSArray *)columns completion:(dispatch_block_t)completion;
- (void)animateBeginGame;
- (void)removeAllCookieSprites;
- (void)BlankPosition:(int) blankPY row:(int)blankPX;


#import "GameScene.h"
#import "Cookie.h"
#import "Level.h"
#import "Swap.h"

static const CGFloat TileWidth = 32.0;
static const CGFloat TileHeight = 36.0;

@interface GameScene ()

@property (strong, nonatomic) SKNode *gameLayer;
@property (strong, nonatomic) SKNode *cookiesLayer;
// record the column and row numbers of the cookie that the player first touched when she started her swipe movement.
@property (assign, nonatomic) NSInteger swipeFromColumn;
@property (assign, nonatomic) NSInteger swipeFromRow;
//add some sound effects to the game.
@property (strong, nonatomic) SKAction *swapSound;
@property (strong, nonatomic) SKAction *invalidSwapSound;
@property (strong, nonatomic) SKAction *matchSound;
@property (strong, nonatomic) SKAction *fallingCookieSound;
@property (strong, nonatomic) SKAction *addCookieSound;

@property (assign, nonatomic) NSInteger test1;
@property (assign, nonatomic) NSInteger test2;

- (id)initWithSize:(CGSize)size {
if ((self = [super initWithSize:size])) {

    self.anchorPoint = CGPointMake(0.5, 0.5);

    SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"Background"];
    [self addChild:background];

    self.gameLayer = [SKNode node];
    self.gameLayer.hidden = YES;
    [self addChild:self.gameLayer];

    CGPoint layerPosition = CGPointMake(-TileWidth*NumColumns/2, -TileHeight*NumRows/2);

    self.cookiesLayer = [SKNode node];
    self.cookiesLayer.position = layerPosition;

    [self.gameLayer addChild:self.cookiesLayer];

    //These properties don´t yet point at any of the cookies
    self.swipeFromColumn = self.swipeFromRow = NSNotFound;

    //These properties don´t yet point to the blank position
    self.test1 = self.test2 = 0;

    //preload sound effects
    [self preloadResources];

return self;}

- (void)BlankPosition:(NSInteger) blankPY row:(NSInteger)blankPX{
NSLog(@" Before: self.test1,  self.test2 %ld, %ld",  (long)self.test1, (long)self.test2);
self.test1 = blankPY;
self.test2 = blankPX;
NSLog(@"blankPY, blankPX %d, %d", blankPY, blankPX);
NSLog(@" After: self.test1,  self.test2 %ld, %ld",  (long)self.test1, (long)self.test2);}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@" touchesBegan: self.test1,  self.test2 %ld, %ld",  (long)self.test1, (long)self.test2);
// It converts the touch location to a point relative to the cookiesLayer.
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self.cookiesLayer];
// it finds out if the touch is inside a square on the level grid. If so, then this might be the start of a swipe motion.
NSInteger column, row;
if ([self convertPoint:location toColumn:&column row:&row]) {

    // the method verifies that the touch is on a cookie rather than on an empty square.
    Cookie *cookie = [self.level cookieAtColumn:column row:row];
    if (cookie != nil) {

        // it records the column and row where the swipe started so you can compare them later to find the direction of the swipe.
        self.swipeFromColumn = column;
        self.swipeFromRow = row;

}    }
Sign In or Register to comment.