In the main loop I call this function to check direction. This should be a select statement and not a bunch of if statements I am working on changing it over.
function playerInput()
if getVirtualButtonPressed(3) = 1
north()
endif
if getVirtualButtonPressed(2) = 1
NW()
endif
if getVirtualButtonPressed(4) = 1
NE()
endif
if getVirtualButtonPressed(5) = 1
west()
endif
if getVirtualButtonPressed(7) = 1
east()
endif
if getVirtualButtonPressed(8) = 1
SW()
endif
if getVirtualButtonPressed(9) = 1
south()
endif
if getVirtualButtonPressed(10) = 1
SE()
endif
endfunction
Here is the setup
//set the speed of the player and direction
posX = 7.5 //12
negX = -7.5 //-12
posY = 5 //10
negY = -5 //-10
in the main loop:
enemyX# = getSpriteX(enemy)
enemyY# = getSpriteY(enemy)
nSpriteX# = getSpriteX(nextPlayer)
nSpriteY# = getSpriteY(nextPlayer)
spriteX# = getSpriteX(player)
spriteY# = getSpriteY(player)
viewX# = screenToWorldX(spriteX)
viewY# = screenToWorldY(spriteY)
currentX# = GetViewOffsetX()
currentY# = GetViewOffsetY()
newX# = currentX#
newY# = currentY#
playerInput()
Here is the main movement with some notes. I was told I shouldn't have this running in a sub loop but I don't know how else to do it and it works great. This is only two directions you repeat for the rest and just change the X/Y for direction.
function north()
if dice > 0 // make sure we still have a number left on the dice
setSpritePhysicsOn(player,2) //turn on the player's physics
playSprite (player, 15,1, 1,8) //play the player's walk animation
do
icnt = GetSpriteFirstContact(player) // check if we hit something
if (icnt > 0) //if we did hit something stop
SetSpritePhysicsVelocity(player,0,.25) // back up just off the wall so we don't have collision right away on next move
SetSpritePhysicsAngularVelocity(player,0) // set this so our player doesn't spin from physics
StopSprite(player) // stop the animation from playing
dice = dice - 1 //loose a number on the die
exit
else
setViewOffset( currentX#, currentY#) //move the screen not the player
if currentY# > currentY# + negY// check grid distance
currentY# = currentY# -.25// move on Y axis this number of units per frame, controls speed
currentX# = currentX# // move on x axis this number of units per frame, controls speed
SetSpritePhysicsVelocity(player,0,-15 ) // move the player at the same speed as the screen movement
SetSpritePhysicsAngularVelocity(player,0) // set this so our player doesn't spin from physics
endif
if currentY# = newY# +negY //Check to see if we moved one full grid space
SetSpritePhysicsVelocity(player,0,0 )// stop the player from moving based on physics
setSpritePhysicsOff(player)//turn off the physics so the character won't get bumped and move and spin.
stopSprite(player)//turn off the walking animation
dice = dice - 1 //subtract one from the dice
exit
endif
endif
sync()
loop
endif
isFacing = North //set the direction the player is now facing
endfunction
function east()
if dice > 0 // make sure we still have a number left on the dice
setSpritePhysicsOn(player,2) //turn on the player's physics
playSprite (player, 15,1, 17,24) //play the player's walk animation
do
icnt = GetSpriteFirstContact(player) // check if we hit something
if (icnt > 0) //if we did hit something stop
SetSpritePhysicsVelocity(player,-.25,0) // back up just off the wall so we don't have collision right away on next player
SetSpritePhysicsAngularVelocity(player,0) // set this so our player doesn't spin from physics
StopSprite(player) // stop the animation from playing
dice = dice - 1 //loose a number on the die
exit
else
setViewOffset( currentX#, currentY#) //player the screen not the player
if currentX# < currentX# + posX// check grid distance
currentY# = currentY# // player on Y axis this number of units per frame, controls speed
currentX# = currentX# + .25 // player on x axis this number of units per frame, controls speed
SetSpritePhysicsVelocity(player,15,0 ) // player the player at the same speed as the screen playerment
SetSpritePhysicsAngularVelocity(player,0) // don't let the sprite spin.
endif
if currentX# = newX# +posX
SetSpritePhysicsVelocity(player,0,0 )
setSpritePhysicsOff(player)
stopSprite(player)
dice = dice - 1
exit
endif
endif
sync()
loop
endif
isFacing = East
endfunction