So Cybermind really thank full for all your examples
I decided I personally like the first button pressed and I get why the need for last button pressed version would be used but I do prefer the first button used.
So I decided to start work on my project however since the start of my project was just the 4 directional movement here is a modular style of code implemented for use in a bigger project with Cybermind's first button pressed system.
here is main.agc
// ****************************************************************
// ****************************************************************
// Title : 4 Directional Movement (main.agc)
// Version : 0.1.0
// Created : Wayne Rayner (aka DaRealWAZY)
// ****************************************************************
// ****************************************************************
// include any addition files
#include "initialise.agc"
#include "playerMovement.agc"
// call Initialise function
Initialise()
do
playerMovement()
Sync()
loop
here is initialise.agc
/* File Name: initialise.agc
Description: Initialise everything!
Set the screen dimensions, set the sync rate, load atlas textures and fonts etc.
Anything that needs an initial value can get set here..
*/
// Create two booleans that give the values 0 and 1 real names (TRUE and FALSE)
#constant TRUE 1
#constant FALSE 0
global displayWidth as integer
global displayHeight as integer
//global atlas as integer
function Initialise()
displayWidth = 1280
displayHeight = 720
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "BattleMonsters" )
SetWindowSize( displayWidth, displayHeight, 0 )
// set display properties
SetVirtualResolution( displayWidth, displayHeight )
SetOrientationAllowed( 0, 0, 1, 1 ) // Landscape mode only
SetSyncRate( 30, 0 ) // Lock the game to 30 frames per second
//LoadFont(1, "Animated.ttf") //Load a font to use for the display. This is a TTF font that is stored in the media folder
//atlas = LoadImage("atlas.png") // Load an atlas image ready for cutting up
//Call the initialse functions in each file
createPlayer()
endfunction
here is playerMovement.agc
/* File Name: playerMovement.agc
Description: Make a character move this will be the code for 4 directional movement (for now will also store player data here but later on that will be moved
*/
// ****************************************************************
// ****************************************************************
// SET UP CONSTANTS
// ****************************************************************
// ****************************************************************
// Set Key Press Constants
#constant keyLeft = GetRawKeyState (37)
#constant keyRight = GetRawKeyState (39)
#constant keyUp = GetRawKeyState (38)
#constant keyDown = GetRawKeyState (40)
#constant keyEsc = GetRawKeyState (27)
// ****************************************************************
// ****************************************************************
// SET UP ALL TYPE SETTINGS
// ****************************************************************
// ****************************************************************
Type playerType
MovementSpeed as integer // create MovementSpeed as varaible
depth as integer // Player depth value which will be set to zero so it is always in front of other sprites
ID as integer
x as integer
y as integer
EndType
global player as playerType
// ****************************************************************
// ****************************************************************
// SET UP GLOBALS
// ****************************************************************
// ****************************************************************
global firstDirection as integer
Function createPlayer()
// Here we create the player sprite, set it's size and colour of RED
// Set it's depth and position it on screen.
// The screen position was set with the map data and the character "&" in that data,
// this can be done using a map editor then and also things like coins, eggs and enemy
// positions can be set easily this way.
// If you look in the text level data you will see "&" somewhere
player.ID = CreateSprite(0)
SetSpriteSize(player.ID, 32, 32)
SetSpriteColor(player.ID, 255, 0, 0, 255)
SetSpritePosition( player.ID, player.x, player.y )
SetSpriteOffset(player.ID, GetSpriteWidth(player.ID)/2, GetSpriteHeight(player.ID)/2)
SetSpriteDepth(player.ID, player.depth)
player.depth = 0 // Set player depth to be on top of other sprites
player.x = 700 // set x position for player
player.y = 300 // set y position for player
EndFunction
Function playerMovement()
player.MovementSpeed = 10
SetSpritePosition( player.ID, player.x, player.y )
// we make the player move by adding or subtracting movement speed from the player.x or player.y position
if keyLeft and firstDirection <> 1
if not keyRight and not keyUp and not keyDown
firstDirection = 1
endif
endif
if keyRight and firstDirection <> 2
if not keyLeft and not keyUp and not keyDown
firstDirection = 2
endif
endif
if keyUp and firstDirection <> 3
if not keyLeft and not keyRight and not keyDown
firstDirection = 3
endif
endif
if keyDown and firstDirection <> 4
if not keyLeft and not keyRight and not keyUp
firstDirection = 4
endif
endif
if not keyLeft and not keyRight and not keyUp and not keyDown then firstDirection = 0
// we set the movement speed to 2 to make sure movement speed returns to 2 after the stopMoving function has been called
if firstDirection = 1 then player.x = player.x - player.MovementSpeed
if firstDirection = 2 then player.x = player.x + player.MovementSpeed
if firstDirection = 3 then player.y = player.y - player.MovementSpeed
if firstDirection = 4 then player.y = player.y + player.MovementSpeed
EndFunction