Validate if the sprite collide/exit the map in one axis at time. If the check return true, then set the speed of the sprite in that axis to 0.
You can check the code of this project [QKYDE4QD] :
- _EUTIL: There is a basic code to handle sprite movement based on collision with layer 3 on the function colBG. This will stop the sprite on that axis if there is a tile on the way.
- __CAMERA: The function setSpeed handle when the camera is out of bound. This stop the backgrounds and the sprite that act as a camera when they would exit from the boundaries defined on the code by the rectangle formed by the points (LX%,LY%),(UX%,UY%).
Also, the camera handle the position as integer. This is done in that way because the BGOFS doesn't work with decimal.
It would be useful if you could write your current implementation (or post a key) so we could see if there is something wrong.
Sprite and BG Movement
Root / Programming Questions / [.]
Waffles_XCreated:
I am trying to move the sprite when it is on a certain area of the screen, but for some reason it does not move. I basically want to enable sprite movement in certain areas of the map like the corners and edges so it does not move off the screen or the bg does not scroll of the screen. The original problem I had was that when I had the bg screen move, the sprite would stop where the bg screen would. I don't know why it does this. Any solutions?
Thanks and here is what I have so far. It involves everything about how the sprite moves.
This is what it looked like when I first noticed the problem:
BGSCREEN 0,128,127 OLD=496 'This is for the direction the sprite will face once it stops moving MOVE=FALSE 'UP=FALSE 'These were about to be implemented instead of functions 'DOWN=FALSE 'LEFT=FALSE 'RIGHT=FALSE S=5 ASP=0 AMAN=496 AX=190 AY=110 SPOFS ASP OUT X,Y SPOFS ASP ASP,AX,AY,AZ BGOFS 0,BGX,BGY BTN=STKANDBTN() IF MOVE=FALSE THEN SPCHR APS,OLD IF BTN==1 AND BTN==17 AND BTN==33 THEN UP IF BTN==2 AND BTN==18 AND BTN==34 THEN DOWN IF BTN==4 AND BTN==20 AND BTN==36 THEN LEFT IF BTN==8 AND BTN==24 AND BTN==40 THEN RIGHT IF BTN==5 AND BTN==21 AND BTN==37 THEN UPLEFT IF BTN==9 AND BTN==25 AND BTN==41 THEN UPRIGHT IF BTN==6 AND BTN==22 AND BTN==38 THEN DOWNLEFT IF BTN==10 AND BTN==26 AND BTN==42 THEN DOWNRIGHT 'The A and B buttons are used. A for attacking and B for speeding up/running IF AX<0 THEN AX=0 IF AX>800 THEN AX=800 IF AY<0 THEN AY=0 IF AY>800 THEN AY=800 IF BGX<0 THEN BGX=0 IF BGX>600 THEN BGX=600 IF BGY<0 THEN BGY=0 IF BGY>768 THEN BGY=768 DEF UP AY=AY-YMOVE BGY=BGY-BGYMOVE ANIM=ANIM+1 IF ANIM==S THEN SPCHR ASP,AMAN+13 IF ANIM==S*2 THEN SPCHR ASP,AMAN+14 IF ANIM==S*3 THEN SPCHR ASP,AMAN+15 IF ANIM==S*4 THEN SPCHR ASP,AMAN+14 OLD=508:MOVE=TRUE 'The function updates the value of the x coordinate variables and counts for how many times it will change the frames. END 'All of the other functions follow this formulaIt was the moment I started implemented bgscreen movement. The sprite does not want to go into the corners or edges of the screen. What I had wrote was working as I wanted it to before.
Ok, I guess the problem is the function UP. You need to check the collision with the boundaries before moving the sprite.
Here is a brief description of what's happening:
1.Sprite is at point 0,0(AX=0,AY=0)
2. You press up for a long time.(AY=-1*number of frame)
4. You press down (AY is still less than 0 so the sprite is stuck. You need to press down for a while until AY is greater than 0)
I suggest you to always set xMove and yMove to 0, and only change them to -1 or 1 if you press the button. Then, after all the input logic, you must put the validations and the movement.
Example:
yMove=0 if button and #UP then yMove=-1 if button and #DOWN then yMove=1 if AY+yMove>0 && AY+yMove<800 then AY=AY+yMove
I cannot understand what is wrong with my code. This is not all of it.
PSP=1 PX=201 'These move the sprite PY=121 PLX=201 'These move the BG PLY=121 XMIN=200 'The min and max distances that the screen can go up, down, left, or right without the screen showing black XMAX=1400 YMIN=120 YMAX=1480 SPSET PSP,PX,PY WHILE TRUE SPD=2 VX=0 VY=0 AX=0 AY=0 BTN=STKANDBTN() 'Calls a function IF BTN AND #UP THEN VY=-SPD ELSEIF BTN AND #DOWN THEN VY=SPD IF BTN AND #LEFT THEN VX=-SPD ELSEIF BTN AND #RIGHT THEN VX=SPD IF (PLY>=0 AND PLY<=YMIN) OR (PLY>=YMAX AND PLY<=1575) THEN VY=0 IF (PLX>=0 AND PLX<=XMIN) OR (PLX>=XMAX AND PLX<=1575) THEN VX=0 IF (PLY>=0 AND PLY<=YMIN) AND (BTN AND #UP) THEN AY=-SPD ELSEIF (PLY>=YMAX AND PLY<=1575) AND (BTN AND #DOWN) THEN AY=SPD IF (PLX>=0 AND PLX<=XMIN) AND (BTN AND #LEFT) THEN AX=-SPD ELSEIF (PLX>=XMAX AND PLX<=1575) AND (BTN AND #RIGHT) THEN AX=SPD PLX=PLX+VX PLY=PLY+VY PX=PX+AX PY=PY+AY VSYNC WENDSo this is supposed to stop the velocities once they get to a certain range, which it does. It does stop the background from moving but the sprite will not move along the x axis when in a corner and only moves up on the y axis and not back down. I only tried it in one corner too (top left). I would also try to debug it myself, but I am confused as to why this doesn't work. Any guidance helping me figure this out is greatly appreciated.