rem Landscape App
SetVirtualResolution ( 800, 600 )
// variables
state = 0
// Create Sprite
CreateSprite ( 1, 0 )
// Set Sprite Position
SetSpritePosition (1, 50, 500)
// Axel RightIdle Frame
AddSpriteAnimationFrame (1,LoadImage ("Sprites\Characters\Player\Idle\Axel-Right-Idle1.png" ) )
AddSpriteAnimationFrame (1,LoadImage ("Sprites\Characters\Player\Idle\Axel-Right-Idle2.png" ) )
AddSpriteAnimationFrame (1,LoadImage ("Sprites\Characters\Player\Idle\Axel-Right-Idle3.png" ) )
AddSpriteAnimationFrame (1,LoadImage ("Sprites\Characters\Player\Idle\Axel-Right-Idle4.png" ) )
// Axel MoveRight Frame
AddSpriteAnimationFrame (1, LoadImage ("Sprites\Characters\Player\RunRight\Axel-RightRun1.png" ) )
AddSpriteAnimationFrame (1, LoadImage ("Sprites\Characters\Player\RunRight\Axel-RightRun2.png" ) )
AddSpriteAnimationFrame (1, LoadImage ("Sprites\Characters\Player\RunRight\Axel-RightRun3.png" ) )
AddSpriteAnimationFrame (1, LoadImage ("Sprites\Characters\Player\RunRight\Axel-RightRun4.png" ) )
AddSpriteAnimationFrame (1, LoadImage ("Sprites\Characters\Player\RunRight\Axel-RightRun5.png" ) )
AddSpriteAnimationFrame (1, LoadImage ("Sprites\Characters\Player\RunRight\Axel-RightRun6.png" ) )
// Play Animation
PlaySprite ( 1, 6, 1, 1, 4 ) // Axel Idle
axelW# = GetSpriteWidth(1)
// Update Screen
speed# = 2.0
do
//Get input
keyState = GetKeyInputState()
// set the correct animation and direction
select keyState
case 0:
if move <> 0 // only set idle animation if it is not already set
PlaySprite ( 1 , 6 , 1 , 1 , 4 ) // Axel Idle
move = 0
endif
endcase
case 1:
if move <> 1 // only set these if not already set
SetSpriteFlip(1, 0, 0)
if move <> 2 // no need to set animation if we were already running
PlaySprite ( 1 , 6 , 1 , 5 , 10 ) // Axel run
endif
dir# = 1.0
move = 1
endif
endcase
case 2:
if move <> 2 // only set these if not already set
SetSpriteFlip(1, 1, 0)
if move <> 1 // no need to set animation if we were already running
PlaySprite ( 1 , 6 , 1 , 5 , 10 ) //Axel run
endif
dir# = -1.0
move = 2
endif
endcase
endselect
//handle movement
if move <> 0
x# = GetSpriteX(1)
//prevent axel from going offscreen
newX# = x# + (speed# * dir#)
if newX# > 1.0 and newX# < (799.0 - axelW#) // extra pixel for any rounding error saftey
SetSpriteX(1 , newX#)
endif
endif
Sync()
loop
function GetKeyInputState()
//wrapping your key inputs into one function will make them easier to maintain
if GetRawKeyState(39) = 1
state = 1
elseif GetRawKeyState(37) = 1
state = 2
else
state = 0
endif
endfunction state
This is the working code.
You had a good start, but were sticking yourself into a corner because of the way you were handling the input. I've made modifications to show you a better way of handling input.
Basic structure should be:
Setup sprites, animations, etc.
Check for input
Set your animation frames
Move
Other actions/effects of the input
A couple other tips that will prevent you from headaches in the future:
Indent your code with every conditional/function - use indenting liberally as well as white space - anything you can do to make your code easier to read, because once you get more than 1,000 lines you'll have a difficult time reading what you've already done and you'll make mistakes because of it.
Use explicit conditionals -
if GetRawKeyState(27) will work, but if confusing because you aren't saying what you expect the output to be in an explicit way.
Always use
if GetRawKeyState(27) = 1 so that you explicitly know what you expect for output. Don't trust that a function will only spit out a 0 or 1 and that a boolean check on the output will work.
Last one -
http://digital-skills.co.uk/agkoverview.html. Order it. It is only $17GBP and have great simple examples that will help you through this and keep programming fun. There are a few outdated examples, but if you search for them on here then you'll find the answers. It's well worth the money and you can probably work through it in just a week. Definitely WORK through it though and don't just read it. It'll make you a happy programmer.