I'm making a 2d fighting. I'm trying to make the sprite attacking detect collision with the one that is idle, but still animating. I've tried detecting collision in multiple sections of the code, but I can't seem to detect any type of collision between my two sprites.
Although I'm drawing the sprite with paste image, the SPRITES are actually behind them, but hidden so i Know that isn't the problem/
Even asking if the sprite detects collision during any one of his animations with the other sprite during anyone of his respective animations there is still no collision.
Here is my code, the collision detection takes place under the collision gosub and under the attack animation under gosub Anim.
Anybody have any suggestions?
`----------------------------------
` GDP FIGHTING GAME
`----------------------------------
`gets rid of sprite image redrawing buffer
backdrop off
`set manual frame rate and manual frame progression
Sync on : sync rate 50
`set resolution
`must be put here so screen height() is accurate
SET DISPLAY MODE 1280,600, 32
`set animated sprite treansparency
SET IMAGE COLORKEY 255,255,255
`set image directory
set dir "C:\Users\ZEUS\Desktop\DarkBasic Project\GDP GAME\Sprites"
gosub Vars
gosub Init
gosub Engine
Vars:
`declare UDT for sprites
Type SPRITET
x as integer
y as integer
id as integer
endtype
TYPE SPRITES
`player x pos
x as integer
`player y pos
y as integer
`player img number
id as integer
`player sprite number
SPR_NUM as integer
`Sets sprite IMG_NUM
IMG_NUM as integer
`start frame
FFrame as integer
`Current Frame
CFrame as integer
`Last Frame
LFrame as integer
`responsible for sprite delay value
SPR_Delay as integer
`Animation
Anim as integer
`Facing right for anim
FRight as integer
`player's health
health as integer
`player's gravity
`Gravity as integer
`jumpspeed
`JumpSpeed as integer
`detects if character is galling
`Falling as integer
`detects if player is jumping
`Jumping as integer
`contains the player's old a max jump info
`JumpMax as integer
EndType
`sets the following as UDT
background as SPRITET
LevelFloor as SPRITET
player1 as SPRITES
player2 as SPRITES
`player1 vars
player1.x = 50
player1.y = screen height() - 90
player1.anim = 0
player1.FRight = 1
player1.health = 100
`player1.gravity = 5
`player1.jumping = 0
`player1.Jumpspeed = 4
`player1.falling = 0
`player1.jumpmax = 450
`player2 vars
player2.x = screen width() - 200
player2.y = 500
player2.anim = 0
player2.FRight = 0
player2.health = 100
`background variables
background.x = 0
background.y = 0
background.id = 1
`floor variables
`Floor variables 2-5 are designated to tiles if necessary
`cannot use "`sprite height" because sprites are noy yet declared
LevelFloor.x = 0
LevelFloor.y = screen height() - 30
LevelFloor.id = 2
return
Init:
`load images
load image "Cubes_Level.jpg", background.id
load image "LevelFloor.jpg", LevelFloor.id
`initialize sprites
`paste sprite background.id, 0, 0
`paste image background.id, 0,0
`Sprite background.id, background.x, background.y, background.id
`level floor is more efficient as a sprite as it does not need to be repasted
`it is also constantly present for more efficient collision detection.
Sprite LevelFloor.id, Levelfloor.x, Levelfloor.y, Levelfloor.id
`Animated Sprites
`zero sprites
create Animated Sprite 10, "Zero_idle_final.bmp", 8, 1, 10
Create Animated Sprite 11, "zero_running_final1.bmp", 16,1, 11
Create Animated Sprite 12, "Zero_attack_Final.bmp", 12, 1, 12
`yuki sprites
Create Animated Sprite 13, "DAN - YUKI IDLE_Final.bmp",8, 1, 13
Create Animated Sprite 14, "DAN - YUKI RUNNING_Final.bmp",10,1,14
Create Animated Sprite 15, "DAN - YUKI KICK_Final.bmp",10,1,15
return
PlayerInput:
`player 1 animations
`idle default anim
player1.Anim = 0
`Walking right
`if rightkey() = 1
if keystate(32) = 1
`here to prevent falling through floor bug if player is moving in the air
`if sprite collision (player1.SPR_NUM, levelfloor.id) = 1
` player1.falling = 0
`endif
`if player1.y > screen height() - 30
` player1.y = screen height() -40
` player1.jumping = 0
` player1.falling = 0
`endif
player1.x = player1.x + 2
`Mirrors Sprite if it's facing left
if player1.FRight = 0
Mirror Sprite 10
Mirror Sprite 11
Mirror Sprite 12
player1.FRight = 1
endif
player1.anim = 1
`yummy spaghetti
gosub anim
`gosub collision
endif
`Walking Left
`if leftkey() = 1
if keystate(30) = 1
`here to prevent falling through floor bug if player is moving in the air
`if sprite collision (player1.SPR_NUM, levelfloor.id) = 1
` player1.falling = 0
`endif
`if player1.y > screen height() - 30
` player1.y = screen height() -37
` player1.falling = 0
` player1.jumping = 0
`endif
player1.x = player1.x - 2
if player1.FRight = 1
Mirror Sprite 10
Mirror Sprite 11
Mirror SPrite 12
player1.FRight = 0
Endif
player1.Anim = 1
`spaghetti code for collision detection
`gosub anim
`gosub collision
endif
`If keystate(17) = 1 and player1.Jumping = 0 and player1.falling = 0
`player1.Jumping = 1
`endif
`if keystate(17) = 0 and player1.Jumping = 1
` player1.jumping = 0
`endif
`more spaghetti code to make colission work properly
`if keystate(17) = 0 and player1.y < player1.Jumpmax
` player1.falling = 1
`endif
`zero attack anim
`prevents attack if moving
if keystate(17) = 1 and keystate(30) <>1 and keystate (32) <> 1
player1.anim = 2
EndIF
`player 2 animations
`idle default anim
player2.Anim = 0
`Walking right
`if rightkey() = 1
if rightkey() = 1
player2.x = player2.x + 2
`Mirrors Sprite if it's facing left
if player2.FRight = 0
Mirror Sprite 13
Mirror Sprite 14
Mirror Sprite 15
player2.FRight = 1
endif
player2.anim = 1
endif
`Walking Left
`if leftkey() = 1
if leftkey() = 1
player2.x = player2.x - 2
if player2.FRight = 1
Mirror Sprite 13
Mirror Sprite 14
Mirror SPrite 15
player2.FRight = 0
Endif
player2.Anim = 1
endif
`attack anim
`prevents attack if moving
if upkey() = 1 and LeftKey() <>1 and RightKey() <> 1
player2.anim = 2
EndIF
Return
Anim:
`player1 Anim
`responsible for animating zero
`animation flag default zero aka idle
If player1.Anim = 0
`positions sprite correctly
player1.y = screen height() - 90
`changes variables responsible for player animation
player1.SPR_NUM = 10
player1.IMG_NUM = 10
player1.FFrame = 1
player1.LFrame = 8
player1.SPR_Delay = 200
play sprite player1.SPR_NUM, player1.FFRAME, Player1.LFrame , player1.SPR_Delay
endif
If player1.Anim = 1
`positions sprite correctly
player1.y = screen height() - 92
player1.SPR_NUM = 11
player1.IMG_NUM = 11
player1.FFrame = 1
player1.LFrame = 16
player1.SPR_Delay = 50
`sprite frame must be one lower than final frame
`as final frame will execute when when sprite frame = 15
if Sprite Frame(player1.SPR_NUM) = 15
player1.FFRAME = 3
endif
play sprite player1.SPR_NUM, player1.FFRAME, Player1.LFrame , player1.SPR_Delay
endif
`resets zero attack nim
if player1.anim <> 2
set sprite frame 12, 1
endif
if player1.Anim = 2
`proper attack height for zero
player1.y = screen height() - 104
player1.SPR_NUM = 12
player1.IMG_NUM = 12
player1.FFrame = 1
player1.LFrame = 11
player1.SPR_Delay = 30
`plays entire attack animation
for x = 1 to 12
player1.FFrame = x
play sprite player1.SPR_NUM, player1.FFRAME, Player1.LFrame , player1.SPR_Delay
paste Sprite player1.SPR_NUM, player1.x,player1.y
if sprite collision (player1.SPR_NUM, player2.SPR_NUM) > 1
player2.health = player2.health - 10
endif
next x
ENDIF
`player2 Anim
`animation flag default idle
If player2.Anim = 0
`positions sprite correctly
player2.y = screen height() - 139
`changes variables responsible for player animation
player2.SPR_NUM = 13
player2.IMG_NUM = 13
player2.FFrame = 1
player2.LFrame = 8
player2.SPR_Delay = 100
play sprite player2.SPR_NUM, player2.FFRAME, Player2.LFrame , player2.SPR_Delay
endif
If player2.Anim = 1
`positions sprite correctly
player2.y = screen height() - 139
player2.SPR_NUM = 14
player2.IMG_NUM = 14
player2.FFrame = 1
player2.LFrame = 10
player2.SPR_Delay = 50
`not necessary for this char as he has no idle to run transition
`sprite frame must be one lower than final frame
`as final frame will execute when when sprite frame = 15
`if Sprite Frame(player1.SPR_NUM) = 9
` player1.FFRAME = 3
`endif
play sprite player2.SPR_NUM, player2.FFRAME, Player2.LFrame , player2.SPR_Delay
endif
`resets zero attack anim
if player2.anim <> 2
set sprite frame 15, 1
endif
if player2.Anim = 2
`proper attack height for zero
player2.y = screen height() - 139
player2.SPR_NUM = 15
player2.IMG_NUM = 15
player2.FFrame = 1
player2.LFrame = 10
player2.SPR_Delay = 30
`plays entire attack animation and checks for collision
for x = 1 to 10
player2.FFrame = x
play sprite player2.SPR_NUM, player2.FFRAME, Player2.LFrame , player2.SPR_Delay
if sprite collision (player2.SPR_NUM, player2.SPR_NUM) > 1
player1.health = player1.health - 10
text 0,0,"SPRITE HIT!"
endif
paste Sprite player2.SPR_NUM, player2.x,player2.y
next x
endif
`play sprite player1.SPR_NUM, player1.FFRAME, Player1.LFrame , player1.SPR_Delay
`sprite player1.SPR_NUM, player1.x, player1.y, 10
return
`Gravity:
` If player1.Falling = 1
` Player1.y = Player1.y + player1.Gravity
` Endif
` If player1.Jumping = 1
` Player1.y = Player1.y - (player1.JumpSpeed*3)
` Endif
` If player1.Y <= player1.JumpMax
` player1.falling = 1
` player1.jumping = 0
` Endif
`Return
Collision:
`should, but doesn't detect collision with the floor
` If Sprite Collision (player1.SPR_NUM, levelFloor.id) = 1
` player1.falling = 0
` player1.Jumping = 0
` player1.Y = Player1.Y
` Endif
`detects for no collision to initialize gravity
` If player1.falling = 0 and Sprite Collision (10, levelfloor.id) = 0
` player1.falling = 1
` Endif
` if player1.y > screen height() - 30
` player1.y = screen height() -30
` endif
` if player1.y > screen height() - 30
` player1.y = screen height() -40
` player1.falling = 0
` endif
if sprite collision (player1.SPR_NUM, player2.SPR_NUM) >1
player2.health = player2.health - 10
text 0,0,"SPRITE HIT!"
endif
if sprite collision (player2.SPR_NUM, player1.SPR_NUM) > 1
player1.health = player1.health - 10
text 0,0,"SPRITE HIT!"
endif
return
healthbar:
`draws healthbar for player1
player1.health = 180
box 10, 10, player1.health, 20, rgb(255,0,0), rgb (255,0,0), rgb(0,255,0), rgb(0,255,0)
`draws healthbar for player2
`the multiplication is done within the draw command so health can be equal
`con: leads to annoying adjustments
player2.health = 180
box 1080,10, player2.health *7, 20, rgb(255,0,0), rgb (255,0,0), rgb(0,255,0), rgb(0,255,0)
return
Engine:
Do
`deletes backdrop which may intefere with colission
cls
`Finds player Old_Y variable for max Jump
`if player1.Jumping = 0
` player1.old_Y = player1.Y
`Endif
`Finds the jumpx max based on players location
`player1.JumpMax = 10
`gets input for character
gosub PlayerInput
`plays animations
gosub Anim
`responsible for gravity
` Gosub Gravity
`detetects collision
gosub Collision
`is responsible for drawing the background
`paste image for BG because it will appear behind sprites
`pasted sprites are printed ontop of BG images
paste image background.id, 0,0
`healthbar
`must be paseted after BG is pasted
gosub healthbar
gosub Collision
sprite levelfloor.id, levelfloor.x, levelfloor.y, levelfloor.id
`rsponsible for drawing player1
sprite player1.SPR_NUM, player1.x, player1.y, player1.SPR_NUM
hide sprite player1.SPR_NUM
paste Sprite player1.SPR_NUM, player1.x,player1.y
hide sprite player1.SPR_NUM
`responsible for drawing player2
sprite player2.SPR_NUM, player2.x, player2.y, player2.SPR_NUM
paste Sprite player2.SPR_NUM, player2.x,player2.y
hide sprite player2.SPR_NUM
`Sprite animation testing
`sprite 12, mousex(), mousey(), 12
`play sprite 12, 1, 12, 12
sync
LOOP
return
---------------------
What the h4x...