INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

2d animation issue with sprites

2d animation issue with sprites

(OP)
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.

RE: 2d animation issue with sprites

If I'm not mistaking

   if rightkey() = 1 and leftkey() = 0
      ryu.spriteNum = 4
      inc ryu.ninjax, ryu.speed
      if ryu.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 ryu.ninjax < 0 then inc ninjax, ryu.speed
      usual()
   endif
   if upkey() = 1 and downkey() = 0
      dec ryu.ninjay, ryu.speed
      if ryu.ninjay < 0 then inc ninjay, ryu.speed
      usual()
   endif
   if downkey() = 1 and upkey() = 0
      inc ryu.ninjay, ryu.speed
      if ryu.ninjay + sprite height(ryu.spriteNum) > 700 then dec ryu.ninjay, ryu.speed
      usual()
   endif
You forgot to put ryu.ninjay instead of ninjay in each if statement.

RE: 2d animation issue with sprites

I forgot to change it in the second part of the if statement too.
if ryu.ninjax < 0 then dec ryu.ninjax

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close