You cannot use -1 here. SPHITSP has 2 arguments which outputs differently. You've used this: SPHITSP(Sprite,Opponent sprite) It checks whether Pac-Man collides with the opponent sprite. It returns TRUE when collision was made and FALSE if it didn't. -1 works as expected if you only use 1 argument as: SPHITSP(Sprite) Then it outputs the opponent sprite ID or -1 if it doesn't. Using 1 argument isn't often viable because if Pacman hits a cherry e.g. he'll ignore the ghosts.... IF SPHITSP(PAC, 1)!=-1 THEN SPOFS 0, OLDX, Y IF SPHITSP(PAC, 1)==-1 THEN X=OLDX ENDIF IF SPHITSP(PAC, 1)!=-1 THEN SPOFS 0,X,OLDY IF SPHITSP(PAC, 1)==-1 THEN Y=OLDY ENDIF 'NOPE DOESN'T WORK
Collision between two sprites
Root / Programming Questions / [.]
AidenCodesCreated:
I'm trying to make a pac man game, but first I need a map. I have all the sprites required for it but when the pac man sprite collides with the map sprites it goes straight through. This is all of my code for it, please don't bother with my POS code other than the collision lol
X=90 Y=90 FRAME=0 ACLS LOAD"GRP4:PAC",0 PACLOOP=0 SPSET PACLOOP OUT PAC SPSET 1, 6 SPOFS 1,36,36 SPSET 2,96,16 SPOFS 2,52,36 SPSET 3,6 SPOFS 3,60,60 SPHOME 3,8,8 SPROT 3,90 SPCOL PAC,TRUE SPCOL 1,TRUE @GAMELOOP SPHOME PAC,8,8 SPOFS PAC,X,Y OLDY=Y OLDX=X IF BUTTON()==#UP THEN Y=Y-2: ROT=90:GOSUB @Pac IF BUTTON()==#DOWN THEN Y=Y+2: ROT=270:GOSUB @Pac IF BUTTON()==#LEFT THEN X=X-2: ROT =0:GOSUB @Pac IF BUTTON()==#RIGHT THEN X=X+2: ROT =180:GOSUB @Pac IF X<8 THEN X=392 IF X>392 THEN X=8 IF Y<8 THEN Y=8 IF Y>232 THEN Y=232 'COLLISION???????? IF SPHITSP(PAC, 1)!=-1 THEN SPOFS 0, OLDX, Y IF SPHITSP(PAC, 1)==-1 THEN X=OLDX ENDIF IF SPHITSP(PAC, 1)!=-1 THEN SPOFS 0,X,OLDY IF SPHITSP(PAC, 1)==-1 THEN Y=OLDY ENDIF 'NOPE DOESN'T WORK GOSUB @Blinky GOSUB @Pinky GOSUB @Inky GOSUB @Clyde IF FRAME==15 THEN FRAME=0 FRAME=FRAME+1 VSYNC GOTO @GAMELOOP @Blinky RETURN @PinkY RETURN @Inky RETURN @Clyde RETURN @Pac IF (FRAME==5 OR FRAME==10 OR FRAME==15) THEN IF PACLOOP==2 THEN SPSET PAC, PACLOOP SPHOME PAC,8,8 SPOFS PAC,X,Y SPROT PAC, ROT PACLOOP=0 ELSE SPSET PAC, PACLOOP PACLOOP=PACLOOP+1 SPHOME PAC,8,8 SPOFS PAC,X,Y SPROT PAC, ROT ENDIF ENDIF RETURN
Ok so now I've edited my code to look like thisYou cannot use -1 here. SPHITSP has 2 arguments which outputs differently. You've used this: SPHITSP(Sprite,Opponent sprite) It checks whether Pac-Man collides with the opponent sprite. It returns TRUE when collision was made and FALSE if it didn't. -1 works as expected if you only use 1 argument as: SPHITSP(Sprite) Then it outputs the opponent sprite ID or -1 if it doesn't. Using 1 argument isn't often viable because if Pacman hits a cherry e.g. he'll ignore the ghosts.... IF SPHITSP(PAC, 1)!=-1 THEN SPOFS 0, OLDX, Y IF SPHITSP(PAC, 1)==-1 THEN X=OLDX ENDIF IF SPHITSP(PAC, 1)!=-1 THEN SPOFS 0,X,OLDY IF SPHITSP(PAC, 1)==-1 THEN Y=OLDY ENDIF 'NOPE DOESN'T WORK
... IF SPHITSP(PAC, 1)==TRUE THEN SPOFS PAC, OLDX, OLDY ENDIFIs this correct?
Well that would only collide with one sprite. There's another option though, with 3 arguments. SPHITSP(sprite, start range, end range) This basically detects collision in a defined range of sprites. I don't remember what it returns, but I think it's aOk so now I've edited my code to look like thisYou cannot use -1 here. SPHITSP has 2 arguments which outputs differently. You've used this: SPHITSP(Sprite,Opponent sprite) It checks whether Pac-Man collides with the opponent sprite. It returns TRUE when collision was made and FALSE if it didn't. -1 works as expected if you only use 1 argument as: SPHITSP(Sprite) Then it outputs the opponent sprite ID or -1 if it doesn't. Using 1 argument isn't often viable because if Pacman hits a cherry e.g. he'll ignore the ghosts.... IF SPHITSP(PAC, 1)!=-1 THEN SPOFS 0, OLDX, Y IF SPHITSP(PAC, 1)==-1 THEN X=OLDX ENDIF IF SPHITSP(PAC, 1)!=-1 THEN SPOFS 0,X,OLDY IF SPHITSP(PAC, 1)==-1 THEN Y=OLDY ENDIF 'NOPE DOESN'T WORK... IF SPHITSP(PAC, 1)==TRUE THEN SPOFS PAC, OLDX, OLDY ENDIFIs this correct?
Well that would only collide with one sprite. There's another option though, with 3 arguments. SPHITSP(sprite, start range, end range) This basically detects collision in a defined range of sprites. I don't remember what it returns, but I think it's aWell right now I only have two sprites down, pac-man and a wall. I just want to get pac-man to stop at this wall so I can expand off of that. I tested the code I made (didn't use it before lol) and pac-man went through the wall still.true/false situation.EDIT: Nope, it returns the colliding sprite ID.
Well I would check whether it's actually detecting collision, by putting a beep in the IF statement or something to listen for.Ok, I did this and the problem is that it won't detect collision. I checked with aIF CONDITION THEN BEEP DOSTUFF ENDIFThen you could figure out from there if the problem is in the IF statement or what it's doing in response.
LOCATE 0,0 PRINT SPHITSP(PAC,1)And it doesn't change when I collide with the other sprite.
I’d recommend going to chat for your problem. It would be a lot faster and you can fix your problem in a few minutes rather than a few days (usually) (sometimes chat is dead).I've tried to check the chat a few times, and it is usually dead. If I saw any activity, I would ask there, but I never see any activity
I've tried to check the chat a few times, and it is usually dead. If I saw any activity, I would ask there, but I never see any activityLol did you get past the /accept when you first enter chat? I tried talking to you earlier today on there and you never responded, and you don’t have any chat stats.
I did before, I was at school when you sent those. lol it didn't show /accept when I did before so I thought it was empty :|I've tried to check the chat a few times, and it is usually dead. If I saw any activity, I would ask there, but I never see any activityLol did you get past the /accept when you first enter chat? I tried talking to you earlier today on there and you never responded, and you don’t have any chat stats.
what are your walls? sprites or bg?Sprites, though I have taken the recommendation of using maps
Just saw you entering and leaving chat a bunch; you still don't have any chat statistics, which means you haven't accepted chat policy yet. if you see a message like this: then read the whole thing, please. If you don't, just send /accept and you'll join chat.I sent /accept, and that was me just checking to make sure of something lol
( ͡° ͜ʖ ͡°)Hmm, seems like our test for seeing if someone could access chat was wrong this entire time. I wonder how many newcomers actually did join chat without us knowing...chat stuffI sent /accept, and that was me just checking to make sure of something lol
Something like this tends to work:
if x1 + w1 > x2 and x1 < x2 + w2 and y1 + h1 > y2 and y1 < y2 + h2 then whateverYouWantHere = trueFor blocks four smaller hit boxes that each do different things one for top, bottom etc. Also and additional check to make sure the player doesnt run past the hitbox, and that only the bottom of the player collides with this hitbox:
if x1 + w1 > x2 and x1 < x2 + w2 and y1 + h1 > y2 and y1 + h1 < y2 + 0.1 + velY and velY > 0 then y1=y2-h1To save memory, only check the hit boxes around the player, like this(assuming tilemap is 16 by 16 in actual space)
startX = floor(playerX / 16) startY = floor(playerY / 16) endX = ceil((playerX + playerW) / 16) endY = ceil((playerY + playerH) / 16) If startX<0 then startX = 0 elseif startX> levelLengthX then startX= levelLengthX 'Same for y For j=startY to endY For i=startX to endX If levelMap[j][i] == "1" then collider i*16,j*16,16,16 Next Next