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 / Making two sprites move simultaneously?

Author
Message
samsonlan
8
Years of Service
User Offline
Joined: 27th Jul 2016
Location: Hawaii
Posted: 27th Jul 2016 09:38
One is the AI that just moves randomly (for now) and the other is the player in which you input movement via buttons. But right now its looping only the AI and I can't move, only in that instant the AI is finished with its movement. Can anyone point me in the right direction? Thanks so much!
JLMoondog
Moderator
15
Years of Service
User Offline
Joined: 18th Jan 2009
Location: Paradox
Posted: 27th Jul 2016 18:31
Each sprite should be assigned a different variable number, that way you can choose which sprite to manipulate in-code by using their variable id #. You can also use a 'name' to represent the variable. Example:



You can use this method to load just about anything, including images, sounds and music. Hope that helps!
samsonlan
8
Years of Service
User Offline
Joined: 27th Jul 2016
Location: Hawaii
Posted: 27th Jul 2016 23:06
Thanks, here's what its doing so far with the do loop. I noticed that with the SpaceShooter demo, the background, player, and aliens are all moving at the same time. But its not the same case with mine. Can you explain why is that or what I should take in mind? Thank you again.

Attachments

Login to view attachments
JLMoondog
Moderator
15
Years of Service
User Offline
Joined: 18th Jan 2009
Location: Paradox
Posted: 28th Jul 2016 04:11
Can you copy and past the code into '' brackets in the post? I can't help unless I see your code.
samsonlan
8
Years of Service
User Offline
Joined: 27th Jul 2016
Location: Hawaii
Posted: 29th Jul 2016 20:28
Ok, I tried to change the loop from do to a while and it kind of works a bit better but its still slow. I attached my code snippet. I allowed my method UpdateEnemy(ene) to return 1. Let me know if I'm missing any more info.


//main loop
while UpdateEnemy(ene) //do
UpdatePlayer(pla)
//UpdateEnemy(ene)
//Sync()
endwhile//loop
JLMoondog
Moderator
15
Years of Service
User Offline
Joined: 18th Jan 2009
Location: Paradox
Posted: 29th Jul 2016 22:05
Gotta post it all man! I have no idea what's in your functions, which I think is what's causing the issues. Also, combining 'while' and 'do' loops is going to really screw with your program in all the wrong ways. Stick with this setup:



Post all codezz!
samsonlan
8
Years of Service
User Offline
Joined: 27th Jul 2016
Location: Hawaii
Posted: 30th Jul 2016 11:26
alrite ok. Here's the rest of the code *** blk is a 2D that contains all the grid spaces the sprites are not allowed to move onto

function UpdatePlayer(pla)
print(GetPointerX())
print(GetPointerY())
Print( ScreenFPS() )
SetTextString(3, "X: " + Str(GetSpriteX(pla),0) + " Y: " + Str(GetSpriteY(pla),0))
Sync()

//player movement
if GetVirtualButtonPressed(1) //up
if CanMove(pla, 1)
for i = 1 to 5
for j = 1 to 10
PlaySprite(pla, 2, 0, 4, 6)
Sync()
next j
SetSpritePosition(pla, GetSpriteX(pla), GetSpriteY(pla)-8)
next i
endif
endif

if GetVirtualButtonPressed(2) //down
if CanMove(pla, 2)
for i = 1 to 5
for j = 1 to 10
PlaySprite(pla, 2, 0, 1, 3)
Sync()
next j
SetSpritePosition(pla, GetSpriteX(pla), GetSpriteY(pla)+8)
next i
endif
endif

if GetVirtualButtonPressed(3) //left
if CanMove(pla, 3)
for i = 1 to 4
for j = 1 to 10
PlaySprite(pla, 2, 0, 7, 9)
Sync()
next j
SetSpritePosition(pla, GetSpriteX(pla)-9, GetSpriteY(pla))
next i
endif
endif


if GetVirtualButtonPressed(4) //right
if CanMove(pla, 4)
for i = 1 to 4
for j = 1 to 10
PlaySprite(pla, 2, 0, 10, 12)
Sync()
next j
SetSpritePosition(pla, GetSpriteX(pla)+9, GetSpriteY(pla))
next i
endif
endif
endfunction

//enemy function
function UpdateEnemy(ene)
true = 1
SetTextString(4, "X: " + Str(GetSpriteX(ene),0) + " Y: " + Str(GetSpriteY(ene),0))

//enemy movement
enedir = Random(1,4)
if CanMove(ene, enedir)
if enedir = 1 //up
for i = 1 to 5 //1 to 5
//for j = 1 to 10
PlaySprite(ene, 2, 0, 5, 6)
Sync()
//next j
SetSpritePosition(ene, GetSpriteX(ene), GetSpriteY(ene)-8) //8
next i
endif
if enedir = 2 //down
for i = 1 to 5
//for j = 1 to 10
PlaySprite(ene, 2, 0, 7, 8)
Sync()
//next j
SetSpritePosition(ene, GetSpriteX(ene), GetSpriteY(ene)+8) //8
next i
endif
if enedir = 3 //left
for i = 1 to 4
//for j = 1 to 10
PlaySprite(ene, 2, 0, 1, 2)
Sync()
//next j
SetSpritePosition(ene, GetSpriteX(ene)-9, GetSpriteY(ene)) //9
next i
endif
if enedir = 4 //right
for i = 1 to 4
//for j = 1 to 10
PlaySprite(ene, 2, 0, 3, 4)
Sync()
//next j
SetSpritePosition(ene, GetSpriteX(ene)+9, GetSpriteY(ene)) //9
next i
endif
endif
endfunction true

function GameOver()
repeat
if win = 1 then print("WIN!")
if win = 0 then print("LOSE!")
Sync()
until GetPointerPressed() > 0
endfunction

function CanMove(i, dir)
IF GetSpriteY(i) = 20 AND dir = 1 //up bounds (lowest y)
move = 0
elseif GetSpriteY(i) = 260 AND dir = 2//down bounds (highest y)
move = 0
elseif (GetSpriteX(i) = 23 OR GetSpriteX(i) = 32) AND dir = 3 //left bounds (lowest x)
move = 0
elseif GetSpriteX(i) = 248 AND dir = 4//right bounds (highest x)
move = 0
else
move = 1 //means can move
endif

//checks for illegal block movements
if dir = 1
plannedy = GetSpriteY(i) - 40
plannedx = GetSpriteX(i)
elseif dir = 2
plannedy = GetSpriteY(i) + 40
plannedx = GetSpriteX(i)
elseif dir = 3
plannedx = GetSpriteX(i) - 36
plannedy = GetSpriteY(i)
elseif dir = 4
plannedx = GetSpriteX(i) + 36
plannedy = GetSpriteY(i)
endif
for a = 1 to blk.length
if plannedx = blk[a,1] AND plannedy = blk[a,2]
move = 0
exit
endif
next a

endfunction move
JLMoondog
Moderator
15
Years of Service
User Offline
Joined: 18th Jan 2009
Location: Paradox
Posted: 31st Jul 2016 05:42
You'll need to give me a moment to look through it, but right away I see a huge problem! Take out all the 'Sync()' commands, every single one of them! You only need one 'sync()', and that's the line just before 'loop'. Only one, no more, no less. Take out all the 'sync()' commands and see if that helps a bit. I'll still go over the codez, and let you know what else I see.
samsonlan
8
Years of Service
User Offline
Joined: 27th Jul 2016
Location: Hawaii
Posted: 31st Jul 2016 11:07
thanks so much! it moves much more fluidly now after I have taken out all the Sync()'s except for that one in UpdatePlayer()! I just have to figure out how to make the monster move slower/normal-looking and have the animations actually work.

Login to post a reply

Server time is: 2024-11-25 02:15:39
Your offset time is: 2024-11-25 02:15:39