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 / Sprite movement from left to right problems

Author
Message
Khadin
5
Years of Service
User Offline
Joined: 13th Feb 2015
Location: Nottingham
Posted: 15th Mar 2015 21:32
Hi all.

Moving the sprite along the xAxis / yAxis isn't a problem, it's when trying to stop it from exiting the screen.

My below code works, but when the sprite hits the side of the wall, there's a 2 second delay before it responds to my imput.

My following code:

Do
xaxis=xaxis+GetDirectionX()*20
yaxis=yaxis+GetDirectionY()*20
SetSpritePosition(ball, xaxis, yaxis)

if (getspritex(ball) < 10)
setspritex(ball, 10)
endif
if (getspritex(ball) > 900)
setspritex(ball, 900)
endif
Sync()
Loop

Also, regarding gravity and physics. Physics only work if i don't set GetDirectionX, GetDirectionY up. Could someone explain how i can move a sprite whilst under the effects of gravity.

Thanks

Khadin
BatVink
Moderator
16
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 16th Mar 2015 08:24
Quote: "if (getspritex(ball) > 900)"


From your left-hand calculation I assume you are trying to stop it 10 units before the side? In which case you need:

rightSide = getVirtualWidth() - 10
if (getspritex(ball) > rightside - getSpriteWidth(ball))


You would calculate rightside just once, somewhere near the top of the program. (This makes it more efficient than calculating every time).

Would that fix your problem? At the moment you are going off the screen because you are checking the left side of the ball. Although I don't know how wide your screen is from the code sample.

Quidquid latine dictum sit, altum sonatur
Funnell7
8
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 16th Mar 2015 10:21
Quote: "Physics only work if i don't set GetDirectionX, GetDirectionY up. "


Moving sprites based on the position, like the below, overrides the Physics engine, as you are forcing the sprite to the location specified.



Instead you will need to move the sprite using one of the physic forces (Impulse, Velocity, Force etc)... My personal preference is to use Velocity SetSpritePhysicsVelocity(Sprite, XForce, YForce)...

Using AppGameKit V2 Tier 1
Mike Archer
5
Years of Service
User Offline
Joined: 19th Feb 2015
Location: Wales
Posted: 16th Mar 2015 12:21
Although you a are limiting the sprites position between 10 and 900, you are allowing the variable xaxis to continue beyond this. After reaching the right hand edge of the screen and pressing left, the delay is because xaxis is over 900 at that point & you won't see any movement until it gets back below 900. So you need to limit the variable, not just the sprite position.
Khadin
5
Years of Service
User Offline
Joined: 13th Feb 2015
Location: Nottingham
Posted: 16th Mar 2015 17:03 Edited at: 16th Mar 2015 17:04
All great answers, thanks guys. I'll check later, but pretty sure that's sorted now. Regarding brackets, both;

if (getspritex(ball) > 900)

and

and: if getspritex(ball) > 900

work. Is there a reason why i should use one over the other?

Khadin
BatVink
Moderator
16
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 16th Mar 2015 18:22
The first may take a microsecond longer

There's no difference, I would guess that Mike uses C#, Java or similar where you put parentheses around the condition.

Quidquid latine dictum sit, altum sonatur
Mike Archer
5
Years of Service
User Offline
Joined: 19th Feb 2015
Location: Wales
Posted: 16th Mar 2015 19:06
I copied the original code that used brackets.

Anyway, there is zero difference to speed as the brackets are evaluated at the time the program is compiled and not when the program is executing.

writing

IF ((x = 1) and (y = 2))

or

IF x = 1 and y =2

will both produce the exact same bytecode file.
Khadin
5
Years of Service
User Offline
Joined: 13th Feb 2015
Location: Nottingham
Posted: 16th Mar 2015 21:22 Edited at: 16th Mar 2015 21:22
Again, thanks guys I have loads of questions, but i'm trying to stagger them a bit lol. I'll work on the physics part, and go from there.

Khadin
Khadin
5
Years of Service
User Offline
Joined: 13th Feb 2015
Location: Nottingham
Posted: 20th Mar 2015 18:41 Edited at: 20th Mar 2015 18:41
Hi again. I've had a week off, so only just had chance to put the above into practice.

None of the above seem to work. It's still hitting the right hand side, then sticking for 1-2 seconds before moving.

Khadin
Khadin
5
Years of Service
User Offline
Joined: 13th Feb 2015
Location: Nottingham
Posted: 20th Mar 2015 19:03 Edited at: 20th Mar 2015 20:50
If it helps, i've started the code off like so:



First time using the Code button, so hope that came out right lol. Now i'm about to do those 'if' statements. That's where i'm struggling.

The above code is different to the original, i decided to start again.

EDIT - Ok, so if i hold the RIGHT arrow key down, for lets say 4 seconds. The ball stays still throughout, but when i come to reverse the direction, then ball sticks for those same 4 seconds before moving, so somehow, i'm still going yet the ball's stationary? Hope you understand what i mean.

EDIT again: Ok, so looks like Mike's code was correct. First time i tried it, the ball would just stick forever, but now for some reason it works. I used the following code:



If you see anything wrong with the code, let me know, and thanks again guys. Unsure why it didn't work the first time Mike, but since i've restarted i now get the desired results!

EDIT = Somehow the ball no longer has any window boundries and just exits the screen lol. I really shouldn't take breaks. Going to re-read about window size, and virtual resolutions, as that's where the problems coming from now.

It works, how i want it to work with the following code:


Question is; Why do i have to type in the actual numbers? I.e. if getspritex(ball) > 700 instead of: if getspritex(ball) > screenW. If i put in 'ScreenW' the ball has no boundry. Glad i sorted out the delay though, cause that was giving me a headache (Thanks again Mike)

Khadin
Mike Archer
5
Years of Service
User Offline
Joined: 19th Feb 2015
Location: Wales
Posted: 20th Mar 2015 21:34
The coordinates are those of the top left corner of the sprite, so "if getspritex(ball) > screenW" will stop when the ball's left hand side has reached the right hand side of the screen, by then the sprite is off the edge of the screen. You actually need to stop it when the right hand side of the sprite reaches the edge of the screen. So to allow for the width of the sprite you would need to write..

if getspritex(ball) + getspritewidth(ball) > screenw
x = screenw - getspritewidth(ball)
setspritex(ball, x)
endif

(I moved setting the variable x first, to save doing the same calculation twice)
Khadin
5
Years of Service
User Offline
Joined: 13th Feb 2015
Location: Nottingham
Posted: 20th Mar 2015 22:08 Edited at: 20th Mar 2015 22:16
Wow thanks Mike. It's 2 steps forward, one step back for me. It's slow going, and when i think i have something down, i realise i don't lol.

EDIT: Tried the code, works perfectly. Thanks again Mike.

Khadin
Mike Archer
5
Years of Service
User Offline
Joined: 19th Feb 2015
Location: Wales
Posted: 22nd Mar 2015 03:31
Learning to program is not just about understanding how to code, it's also about the harder skill of learning to spot your mistakes, that one's more tricky, and that's what these forums are for

Login to post a reply

Server time is: 2020-02-20 09:38:50
Your offset time is: 2020-02-20 09:38:50