Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / move player with keyboard

Author
Message
chfire
16
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 17th Nov 2016 21:56
Hi Everyone

I am new to Programing and Appgamekit. I am having trouble moving the " player " with the keyboard. wondering if someone can put me in the right direction.

// Project: timapk
// Created: 2016-11-16

// set window properties
SetWindowTitle( "timapk" )
SetWindowSize( 1024, 768, 0 )

// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
LoadImage (1, "player.png" )
createsprite(1)


SetSpritePosition ( 1, 440, 200 )


Do







if getrawkeystate(37)
y = y - 5
endif


if Getrawkeystate(39)
y = y + 5
endif

if Getrawkeystate(32)
x = x + 5
endif


sync()

loop

=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 18th Nov 2016 08:36
You are only changing the Coordinate variables. You also need to reposition the player each loop.
damothegreat
User Banned
Posted: 18th Nov 2016 13:10
put

setspriteposition (1, x, y )

As your using the X and Y variables in increments of five, need to tell AppGameKit where the sprite needs to live on the screen by using X and Y in the sprite position command

Put this at the top of the main loop, just after the DO command

Damo
Using Tier 1 AppGameKit V2
Started coding with AMOS
hoyoyo80
8
Years of Service
User Offline
Joined: 11th May 2016
Location:
Posted: 21st Nov 2016 04:13 Edited at: 21st Nov 2016 05:37
if key(right) then speedX#=0.1
if key(left) then speedX#=-0.1
if key(up) then speedY#=0.1
if key(down) then speedY#=-0.1
setspriteposition(1,getspriteX(1)+speedX#,getspriteY(1)+speedY#))

this way sprite movement can be control by speedX# and speedY# variables.
chfire
16
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 22nd Nov 2016 00:12
Sorry it took so long to reply! Had a weekend trip to Disney "Epcot ".
Thanks everyone for the help! I did get it to work with these changes.

// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
LoadImage (1, "player.png" )
createsprite(1,1)
x=440
y=200

Do
SetSpritePosition ( 1, x,y )
if getrawkeystate(37)
X = X - 5
endif
if Getrawkeystate(39)
X = X + 5
endif
sync()

loop

I will need to adjust the speed of the player. hoyoyo I did try your code and got some errors. I think I needed to setup the speedx, speedy, made them GLOBAL but not sure what I am doing.
Just trying to make a space invaders game as a learning experience. The SetSpritePosition ( 1, x,y ) and x=440 y=200 is what fixed my issue. Thanks again everyone!
damothegreat
User Banned
Posted: 22nd Nov 2016 14:49 Edited at: 22nd Nov 2016 19:15
Try this - implemented the idea from what Hoyoyo80 trying to achieve and a little more to help you






Added a little boost image to it too, create like a flame that comes out of a rocket when accellerates or such like first and call it boost.png

f like anymore help with anything, give us a shout

perhaps create a sprite that looks like a star and replace the drawline commands (that create the star) with setspritesize commands
Hope it helps

Enjoy!!

Damo
Using Tier 1 AppGameKit V2
Started coding with AMOS
Anything is possible if put mind to it
chfire
16
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 22nd Nov 2016 22:51
damothegreat thanks for the code! But it's just a little beyond my understanding right now. now I am having a issue keeping the " player " from moving off the screen.
added 2 lines to the code and it works, But there is a two second pause when the player reaches the edge of the screen and is able to turn back.
Also the movement is not smooth. assuming the speedx & speedy will fix this?

Do

SetSpritePosition ( 1, x,y )

if getrawkeystate(37)
X = X - 15
endif

if Getrawkeystate(39)
X = X + 15
endif
REM NEW CODE-----------------------------------------

if x < 20 then setspriteposition (1,20,600)

if x > 900 then SetSpritePosition (1,900,600)
REM --------------------------------------------------------------
sync()

loop

damothegreat
User Banned
Posted: 22nd Nov 2016 23:03
Tried your code - and it jumps all over the place

Is this what your trying to do



Damo
Using Tier 1 AppGameKit V2
Started coding with AMOS
Anything is possible if put mind to it
chfire
16
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 23rd Nov 2016 00:18
Hi Damo, Yes That's what I am trying to do. your code works just like mine. " two second delay " do images have a size limitation? size restrictions, power of 2.
The player image is 132 x 205 png format.
damothegreat
User Banned
Posted: 23rd Nov 2016 01:01
I creates a sprite this size, and I just cannot seem to replicate the 2 second delay your experiencing with this code.

No limitations on sprites, all sizes should work great - but always nicer to indeed create power of 2 ones.

could you copy and paste all of the code in full that you have at the moment

pop it inside ..... /code] tags, so it better formulates on here

Damo



Using Tier 1 AppGameKit V2
Started coding with AMOS
Anything is possible if put mind to it
chfire
16
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 23rd Nov 2016 22:17 Edited at: 23rd Nov 2016 22:19
I copied part of your code and it worked the same. now I used ALL of you code and it works fine.
So I am wondering if these two lines had something to do with the issue.

damothegreat
User Banned
Posted: 23rd Nov 2016 22:33
correct it is these two

Within your DO loop - you have specified that the sprite is moved using X and Y (first command straight after the DO command)


and also changing the location with the X and Y variables only

These two IF conditions are specifying a sprite location without the use of X and Y variable - so to AppGameKit - it doesn't know where it is.

So replacing the setspritepostion (1,20,600) and setspriteposition(1,900,600) to X = 20 and X=900 and leave the Y alone (cause don't want it moving
vertically - that will do the trick and effect your after

Hope it makes sense - been a long day

Damo
Using Tier 1 AppGameKit V2
Started coding with AMOS
Anything is possible if put mind to it
chfire
16
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 25th Nov 2016 02:46
it makes sense! and thanks for the help! it's much appreciated
hoyoyo80
8
Years of Service
User Offline
Joined: 11th May 2016
Location:
Posted: 25th Nov 2016 06:10
To track the position of sprite it good to use getspritex or getspritey

Login to post a reply

Server time is: 2024-11-21 21:29:30
Your offset time is: 2024-11-21 21:29:30