Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm making a very simple Pac - Man game and have everything working except for a small bug. I'm using a grid based system where Pac - Man can only move if the tiles in front of him are open. Everything works fine up until you make a turn. If you turn before Pac - Man is in the right tile, his location gets rounded out and he ends up moving through the wall. I've added my code and an image of what happens.

Player.as class

package 
{
import flash.display.MovieClip;
import flash.display.InteractiveObject;

public class Player extends MovieClip
{
    var speed:int;
    var playerX:int;
    var playerY:int;
    var wall:Wall = new Wall  ;
    var currentKey:String;
    var playerRX:int;
    var playerRY:int;
    public function Player()
    {
    }

    public function movePlayer(lastKey:String, levelArray:Array)
    {
        trace(currentKey, lastKey)
        if(currentKey == lastKey)
        {
            if (x % wall.width >= speed)
            {
                playerRX = 1;
            }
            else
            {
                playerRX = 0;
            }
            if(y % wall.width >= speed)
            {
                playerRY = 1
            }
            else
            {
                playerRY = 0;
            }
        }
        currentKey = lastKey;
        speed = 5;
        playerX = x / 15;
        playerY = y / 15;
        var leftCollision = playerX + playerRX;
        var topCollision = playerY + playerRY;
        if (lastKey == "left")
        {
            if (levelArray[leftCollision - 1][playerY] != 1 && levelArray[leftCollision - 1][playerY + 1] != 1 && levelArray[leftCollision - 1][playerY + 2] != 1)
            {
                x -=  speed;
            }
        }
        if (lastKey == "right")
        {
            if (levelArray[playerX + 3][playerY] != 1 && levelArray[playerX + 3][playerY + 1] != 1 && levelArray[playerX + 3][playerY + 2] != 1)
            {
                x +=  speed;
            }
        }
        if (lastKey == "up")
        {
            if (levelArray[playerX][topCollision - 1] != 1 && levelArray[playerX + 1][topCollision - 1] != 1 && levelArray[playerX + 2][topCollision - 1] != 1)
            {
                y -=  speed;
            }
        }
        if (lastKey == "down")
        {
            if (levelArray[playerX][playerY + 3] != 1 && levelArray[playerX + 1][playerY + 3] != 1 && levelArray[playerX + 2][playerY + 3] != 1)
            {
                y +=  speed;
            }
        }
    }
}
}

The bug

Currently Pac - Man doesn't move from tile to tile, Pac - Man is 45x45 and tiles are 15x15. Should i change it so he does move from tile to tile and tween it to make it look like hes transitioning?

share|improve this question
You should check if he is going to collide before he moves. Are you doing that already? – Gilson Jul 16 '12 at 11:38
Yes, the really long if conditions do that. The problem is with the rounding Pac - Man to a grid. – inzombiak Jul 16 '12 at 11:40
Have you seen this topic? – David Gouveia Jul 16 '12 at 12:08
I hadn't, but I'm not sure if I understand it fully. Are you saying I should set "goals" for the movement and once a goal is reached be allowed to turn? Like if a path has 3 points from where I can turn, I should set the goal to the closest one and once I reach it allow the player to turn and if he does I set a new goal in the new path and if he doesn't I just move the goal to the next intersection in the path? – inzombiak Jul 16 '12 at 12:34
1  
Yes :) The only thing the player can do is change direction in the moments where pacman is aligned with the grid (although you can detect the key press earlier and store it, so that it only activates at the next tile), and when the tile in the direction he pressed happens to be free. Other than that it's just tweening the position between tiles. – David Gouveia Jul 16 '12 at 15:02
show 4 more comments

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.