Skip to content

Out of screen bug fixed (i think) #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions m2048/Sprite Kit/M2Tile.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ - (NSInteger)mergeToTile:(M2Tile *)tile

// 3. Update value and pop.
[self updateLevelTo:newLevel];
[_pendingActions addObject:[self pop]];

[self runAction:[self pop]];
}
return newLevel;
}
Expand All @@ -153,7 +154,8 @@ - (NSInteger)merge3ToTile:(M2Tile *)tile andTile:(M2Tile *)furtherTile

// 3. Update value and pop.
[self updateLevelTo:newLevel];
[_pendingActions addObject:[self pop]];

[self runAction:[self pop]];
}
return newLevel;
}
Expand Down Expand Up @@ -181,11 +183,13 @@ - (void)refreshValue

- (void)moveToCell:(M2Cell *)cell
{
[_pendingActions addObject:[SKAction moveBy:[GSTATE distanceFromPosition:self.cell.position
toPosition:cell.position]
duration:GSTATE.animationDuration]];
SKAction * move = [SKAction moveBy:[GSTATE distanceFromPosition:self.cell.position
toPosition:cell.position]
duration:GSTATE.animationDuration];
self.cell.tile = nil;
cell.tile = self;

[self runAction:move];
}


Expand Down
59 changes: 34 additions & 25 deletions m2048/State/M2GameManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ BOOL iterate(NSInteger value, BOOL countUp, NSInteger upper, NSInteger lower) {
return countUp ? value < upper : value > lower;
}


@implementation M2GameManager {
/** True if game over. */
BOOL _over;
Expand All @@ -46,47 +45,57 @@ @implementation M2GameManager {
M2Grid *_grid;

/** The display link to add tiles after removing all existing tiles. */
CADisplayLink *_addTileDisplayLink;
// CADisplayLink *_addTileDisplayLink;
}


# pragma mark - Setup

- (void)startNewSessionWithScene:(M2Scene *)scene
{
if (_grid && _grid.dimension == GSTATE.dimension) {
// If there is an existing grid and its dimension is still valid,
// we keep it, only removing all existing tiles with animation.
[_grid removeAllTilesAnimated:YES];
} else {
if (_grid) [_grid removeAllTilesAnimated:NO];
_grid = [[M2Grid alloc] initWithDimension:GSTATE.dimension];
_grid.scene = scene;
}
// if (_grid && _grid.dimension == GSTATE.dimension) {
// // If there is an existing grid and its dimension is still valid,
// // we keep it, only removing all existing tiles with animation.
// [_grid removeAllTilesAnimated:YES];
// } else {
// if (_grid) [_grid removeAllTilesAnimated:NO];
// _grid = [[M2Grid alloc] initWithDimension:GSTATE.dimension];
// _grid.scene = scene;
// }

// Restart bug still occure, this seems to fix the PB
if (_grid)
[_grid removeAllTilesAnimated:NO];
_grid = [[M2Grid alloc] initWithDimension:GSTATE.dimension];
_grid.scene = scene;


[scene loadBoardWithGrid:_grid];

// Set the initial state for the game.
_score = 0; _over = NO; _won = NO; _keepPlaying = NO;

// Bug on restart if the scene is full, this seens to fix it
[_grid insertTileAtRandomAvailablePositionWithDelay:NO];
[_grid insertTileAtRandomAvailablePositionWithDelay:NO];

// Existing tile removal is async and happens in the next screen refresh, so we'd wait a bit.
_addTileDisplayLink = [CADisplayLink displayLinkWithTarget:self
selector:@selector(addTwoRandomTiles)];
[_addTileDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
// _addTileDisplayLink = [CADisplayLink displayLinkWithTarget:self
// selector:@selector(addTwoRandomTiles)];
// [_addTileDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}


- (void)addTwoRandomTiles
{
// If the scene only has one child (the board), we can proceed with adding new tiles
// since all old ones are removed. After adding new tiles, remove the displaylink.
if (_grid.scene.children.count <= 1) {
[_grid insertTileAtRandomAvailablePositionWithDelay:NO];
[_grid insertTileAtRandomAvailablePositionWithDelay:NO];
[_addTileDisplayLink invalidate];
}
}

//- (void)addTwoRandomTiles
//{
// // If the scene only has one child (the board), we can proceed with adding new tiles
// // since all old ones are removed. After adding new tiles, remove the displaylink.
// if (_grid.scene.children.count <= 1) {
// [_grid insertTileAtRandomAvailablePositionWithDelay:NO];
// [_grid insertTileAtRandomAvailablePositionWithDelay:NO];
// [_addTileDisplayLink invalidate];
// }
//}

# pragma mark - Actions

Expand Down
6 changes: 3 additions & 3 deletions m2048/m2048-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>me.danqing.${PRODUCT_NAME:rfc1034identifier}</string>
<string>me.danqing.$(PRODUCT_NAME:rfc1034identifier)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.1</string>
<string>1.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>4</string>
<string>5</string>
<key>LSApplicationCategoryType</key>
<string></string>
<key>LSRequiresIPhoneOS</key>
Expand Down