I have a problem. If you'll look at the following code you'll see that what I have now is pretty good right now (in my eyes anyway). However, in those rightkey(), downkey(), upkey() and downkey() functions you'll notice I'm trying to keep the sprite within the boundaries of an imaginary box. However, currently the boundaries are not functioning properly for me. Can someone try out the code to see what I'm missing or doing wrong? Thanks.
hide mouse
set display mode 950, 600, 16
sync on : cls
sr = screen fps()
sync rate sr
set window on
set window size 950,600
set window layout 1,3,1
set window position 5,5
load image "NG_standing_left.bmp", 1
load image "NG_standing_right.bmp", 2
create animated sprite 3, "NG running left.bmp", 3, 1, 3
create animated sprite 4, "NG running right.bmp", 3, 1, 4
TYPE ninja
ninjax
ninjay
scale
speed
spriteNum
endtype
global ryu as ninja
ryu.ninjax = 250
ryu.ninjay = 250
ryu.scale = 25
ryu.speed = 5
ryu.spriteNum = 3
sprite 2,ryu.ninjax,ryu.ninjay,2
show sprite 2
do
directions() `shows directions from right, left, up, down
loop
delete image 1
delete image 2
delete image 3
delete image 4
function directions()
if rightkey() = 1 and leftkey() = 0
ryu.spriteNum = 4
inc ryu.ninjax, ryu.speed
if ninjax + sprite width(4) > 600 then dec ryu.ninjax, ryu.speed
usual()
endif
if leftkey() = 1 and rightkey() = 0
ryu.spriteNum = 3
dec ryu.ninjax, ryu.speed
if ninjax < 0 then inc ninjax, ryu.speed
usual()
endif
if upkey() = 1 and downkey() = 0
dec ryu.ninjay, ryu.speed
if ninjay < 0 then inc ninjay, ryu.speed
usual()
endif
if downkey() = 1 and upkey() = 0
inc ryu.ninjay, ryu.speed
if ninjay + sprite height(ryu.spriteNum) > 700 then dec ryu.ninjay, ryu.speed
usual()
endif
if rightkey()=0 and leftkey()=0 and upkey()=0 and downkey()=0 then ninja_standing()
if (rightkey()=1 and leftkey()=1) or (upkey()=1 and downkey()=1) then ninja_standing()
ink rgb(255,255,255), rgb(0,0,0)
set text size 10
set cursor 0,0
print "X: " + str$(ryu.ninjax) + " Y: " + str$(ryu.ninjay)
endfunction
function ninja_standing()
hide all sprites
sprite ryu.spriteNum-2, ryu.ninjax+40, ryu.ninjay, ryu.spriteNum-2
show sprite ryu.spriteNum-2
scale sprite ryu.spriteNum-2, ryu.scale
sync : cls
endfunction
function usual()
hide all sprites
show sprite ryu.spriteNum
sprite ryu.spriteNum, ryu.ninjax, ryu.ninjay, ryu.spriteNum
scale sprite ryu.spriteNum, ryu.scale
play sprite ryu.spriteNum, 1, 3, 200
sync : cls
endfunction
By the way, making the type "ninja" as global was the only way I could find to make spriteNum work for me. Before I made it global I was passing it through the functions but for some reason whenever the leftkey() function wasn't being called the program it would inherit the original value of spriteNum (occurring before the loop) from within the loop. That baffled me for hours and I decided to go global with it.
hide mouse
set display mode 950, 600, 16
sync on : cls
sr = screen fps()
sync rate sr
set window on
set window size 950,600
set window layout 1,3,1
set window position 5,5
load image "NG_standing_left.bmp", 1
load image "NG_standing_right.bmp", 2
create animated sprite 3, "NG running left.bmp", 3, 1, 3
create animated sprite 4, "NG running right.bmp", 3, 1, 4
TYPE ninja
ninjax
ninjay
scale
speed
spriteNum
endtype
global ryu as ninja
ryu.ninjax = 250
ryu.ninjay = 250
ryu.scale = 25
ryu.speed = 5
ryu.spriteNum = 3
sprite 2,ryu.ninjax,ryu.ninjay,2
show sprite 2
do
directions() `shows directions from right, left, up, down
loop
delete image 1
delete image 2
delete image 3
delete image 4
function directions()
if rightkey() = 1 and leftkey() = 0
ryu.spriteNum = 4
inc ryu.ninjax, ryu.speed
if ninjax + sprite width(4) > 600 then dec ryu.ninjax, ryu.speed
usual()
endif
if leftkey() = 1 and rightkey() = 0
ryu.spriteNum = 3
dec ryu.ninjax, ryu.speed
if ninjax < 0 then inc ninjax, ryu.speed
usual()
endif
if upkey() = 1 and downkey() = 0
dec ryu.ninjay, ryu.speed
if ninjay < 0 then inc ninjay, ryu.speed
usual()
endif
if downkey() = 1 and upkey() = 0
inc ryu.ninjay, ryu.speed
if ninjay + sprite height(ryu.spriteNum) > 700 then dec ryu.ninjay, ryu.speed
usual()
endif
if rightkey()=0 and leftkey()=0 and upkey()=0 and downkey()=0 then ninja_standing()
if (rightkey()=1 and leftkey()=1) or (upkey()=1 and downkey()=1) then ninja_standing()
ink rgb(255,255,255), rgb(0,0,0)
set text size 10
set cursor 0,0
print "X: " + str$(ryu.ninjax) + " Y: " + str$(ryu.ninjay)
endfunction
function ninja_standing()
hide all sprites
sprite ryu.spriteNum-2, ryu.ninjax+40, ryu.ninjay, ryu.spriteNum-2
show sprite ryu.spriteNum-2
scale sprite ryu.spriteNum-2, ryu.scale
sync : cls
endfunction
function usual()
hide all sprites
show sprite ryu.spriteNum
sprite ryu.spriteNum, ryu.ninjax, ryu.ninjay, ryu.spriteNum
scale sprite ryu.spriteNum, ryu.scale
play sprite ryu.spriteNum, 1, 3, 200
sync : cls
endfunction
By the way, making the type "ninja" as global was the only way I could find to make spriteNum work for me. Before I made it global I was passing it through the functions but for some reason whenever the leftkey() function wasn't being called the program it would inherit the original value of spriteNum (occurring before the loop) from within the loop. That baffled me for hours and I decided to go global with it.