LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

How to make a motion border for sprites with if statments?

Root / Programming Questions / [.]

Techster14Created:
i'm having issues figuring this out. I have code that makes at sprite move but I want to incorporate a viewless boarder so that when the sprite go the edge of the play area provided, it's based on a chess board, the sprite will stop moving but it will still move away from the border, in other words, I want movement code that includes only the dimensions of the checkered area only and makes the sprite only allowed to move in that area. How could I do that? Also, should I include what I have coded?

Can you please rephrase your question? I'm confused at what you are asking.

uh is this what you're looking for?
IF PX%<BSX% THEN PX%=BSX%
IF PX%>BEX% THEN PX%=BEX%
IF PY%<BSX% THEN PY%=BSY%
IF PY%>BEX% THEN PY%=BEY%
PX% is the sprite position x coordinates. PY% is the Y coordinates. BSX% and BSY% are the lower x and y limitations. So you could set BSX% to 72 and it the sprite couldn't go anywhere to the left of that, and the same goes for BSY% except the sprite can't go above it. With BEX% and BEY%, it's the higher limitation. So the sprite can't go to the right of BEX% and it can't go below BEY%. I hope this helps.

My code is
’up
If button(0)==1 then
Spofs 1, x,y-k
Endif
‘Down
If button(0)==2 then
Spofs 1,x,y+k
Endif
‘Left
If button(0)==4 then
Spofs 1,x-k,y
Endif
‘Right
If button(0)==8 then
Spofs 1,x+k,y
Endif
How would i set the movement area border parameters into this code? K represents the “slope” or change in position of the x or y values.

Also if possible, can someone teach me how to use collision testing? I’m wanting to code in an enemy sprite that will cause damage to your sprite if they collide and i can’t figure out how it works.

Whoa there friend, I think you need to slow down. How long exactly have you been using SmileBASIC?

He's probably new. BTW if you want to detect multiple button presses, use
BUTTON(0) AND #A

I’ve been using smileBASIC for 2 years now so yeah i’m still a newb but i’ve made a cmd prompt and other things.

He's probably new. BTW if you want to detect multiple button presses, use
BUTTON(0) AND #A
What does the
#A
do? Also, will that allow me to condense my movement system to one if statement?

#A means 16, or the button A.

Oh, ok, just making sure. So i could do
BUTTON(0) AND #UP
and it will allow my sprite to move if the up button is pressed?

Yes.

As the parameters of your 'if' statement, yes. It would look something like this:
IF BUTTON(0) AND #RIGHT THEN playerX=playerX+1
Or
IF BUTTON(0) AND #RIGHT THEN
playerX=playerX+1
ENDIF

Yeah it does need to be in an IF statement.

I know it needs to be in an if statement, since all computer programs simplify to basic logic.

Back to your original question, are you trying to keep a sprite from moving out of bounds, not allow it to move while its out of bounds, or something else?

I’m trying to prevent the sprite from going out of bounds.

uh is this what you're looking for?
IF PX%<BSX% THEN PX%=BSX%
IF PX%>BEX% THEN PX%=BEX%
IF PY%<BSX% THEN PY%=BSY%
IF PY%>BEX% THEN PY%=BEY%
PX% is the sprite position x coordinates. PY% is the Y coordinates. BSX% and BSY% are the lower x and y limitations. So you could set BSX% to 72 and it the sprite couldn't go anywhere to the left of that, and the same goes for BSY% except the sprite can't go above it. With BEX% and BEY%, it's the higher limitation. So the sprite can't go to the right of BEX% and it can't go below BEY%. I hope this helps.
The solution was given to you by random.

So then my code should look like this:
if button(0) and (#up or #down or #right or #left) then {insert move code here}
Endif
While true:
If px%<bs%...

So then my code should look like this:
if button(0) and (#up or #down or #right or #left) then {insert move code here}
Endif
While true:
If px%<bs%...
The problem with this is that all of the button inputs are in one IF statement, and it would be hard to figure out which way it should go. It's better to use 4 IF statements.