473,395 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Sprite Movement using arrow keys

Hello,
I have been working on a dragonball z game recently and I have encountered a problem with the sprites.I have traced the bitmap and everything else and even created a movieclip of the sprite.I have put this code on the movieclip-
Expand|Select|Wrap|Line Numbers
  1. onClipEvent (load) {
  2.     step = 5;
  3. }
  4. onClipEvent (enterFrame) {
  5.     if (Key.isDown(Key.RIGHT) && this._x<550) {
  6.         this._x += step;
  7.         gotoAndPlay(2);
  8.     } else if (Key.isDown(Key.LEFT) && this._x>0) {
  9.         this._x -= step;
  10.         gotoAndPlay(10);
  11.     } else if (Key.isDown(Key.UP) && this._y>0) {
  12.         this._y -= step;
  13.         gotoAndPlay(15);
  14.     } else if (Key.isDown(Key.DOWN) && this._y<400) {
  15.         this._y += step;
  16.         gotoAndPlay(6);
  17.     }
  18. }
I have put animations of the character walking on the differnet frames inside the movieclip.For eg-
I have shown the character walking right from frame 2-5,walking down from frame 6-9,walking left from frame10-14,walking up from frame 15-19.
THE PROBLEM is that whenever I press a specefic key(I will take the example of right) and keep it pressed,IT DOES NOT PLAY THE WHOlE ANI:MATION OF MOVING RIGHT.It just plays frame 2 and repeats it instead of playing frames 2-5 together.
HELP is required!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Jun 29 '07 #1
2 5811
xNephilimx
213 Expert 100+
Hello,
I have been working on a dragonball z game recently and I have encountered a problem with the sprites.I have traced the bitmap and everything else and even created a movieclip of the sprite.I have put this code on the movieclip-
Expand|Select|Wrap|Line Numbers
  1. onClipEvent (load) {
  2.     step = 5;
  3. }
  4. onClipEvent (enterFrame) {
  5.     if (Key.isDown(Key.RIGHT) && this._x<550) {
  6.         this._x += step;
  7.         gotoAndPlay(2);
  8.     } else if (Key.isDown(Key.LEFT) && this._x>0) {
  9.         this._x -= step;
  10.         gotoAndPlay(10);
  11.     } else if (Key.isDown(Key.UP) && this._y>0) {
  12.         this._y -= step;
  13.         gotoAndPlay(15);
  14.     } else if (Key.isDown(Key.DOWN) && this._y<400) {
  15.         this._y += step;
  16.         gotoAndPlay(6);
  17.     }
  18. }
I have put animations of the character walking on the differnet frames inside the movieclip.For eg-
I have shown the character walking right from frame 2-5,walking down from frame 6-9,walking left from frame10-14,walking up from frame 15-19.
THE PROBLEM is that whenever I press a specefic key(I will take the example of right) and keep it pressed,IT DOES NOT PLAY THE WHOlE ANI:MATION OF MOVING RIGHT.It just plays frame 2 and repeats it instead of playing frames 2-5 together.
HELP is required!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Your sprite's animation is not playing because when you press a key, remember you are in an onEnterFrame event, so every frame the gotoAndPlay function will try to go from the frame you specified, making every frame that frame, so it's never going to play the animation. That's logical, though sometimes difficult to see the error. you gotta do this:

"guy" is the instance name of my sprite
I wrote all the code in a main timeline frame, you could do it in an external as file and include it in the frame instead, if you have lot's of code.
You should try to write all the code separately, so you won't have parts of code lost in movieclips inside movieclips inside movieclips..... etc.

you can check this working example at http://www.thenephilim.com.ar/guy.swf
and download it from http://www.thenephilim.com.ar/guy.fla

Expand|Select|Wrap|Line Numbers
  1. var step:Number = 8;
  2. var flipped = -guy._xscale;
  3.  
  4. onEnterFrame = function() {
  5.     if (Key.isDown(Key.RIGHT)) {
  6.         guy._xscale = -flipped;
  7.         guy._x += step;
  8.  
  9.         if(guy._currentframe < 6) {
  10.             guy.gotoAndPlay(6);
  11.         }
  12.         if(guy._currentframe < 14) {
  13.             guy.nextFrame();
  14.         } else {
  15.             guy.gotoAndPlay(6);
  16.         }
  17.     } else if (Key.isDown(Key.LEFT)) {        
  18.         guy._xscale = flipped;
  19.  
  20.         guy._x -= step;
  21.         if(guy._currentframe < 6) {
  22.             guy.gotoAndPlay(6);
  23.         }
  24.         if(guy._currentframe < 14) {
  25.             guy.nextFrame();
  26.         } else {
  27.             guy.gotoAndPlay(6);
  28.         }
  29.     } else {
  30.         guy.gotoAndStop(1);
  31.     }
  32. }
  33.  
Jun 29 '07 #2
Your sprite's animation is not playing because when you press a key, remember you are in an onEnterFrame event, so every frame the gotoAndPlay function will try to go from the frame you specified, making every frame that frame, so it's never going to play the animation. That's logical, though sometimes difficult to see the error. you gotta do this:

"guy" is the instance name of my sprite
I wrote all the code in a main timeline frame, you could do it in an external as file and include it in the frame instead, if you have lot's of code.
You should try to write all the code separately, so you won't have parts of code lost in movieclips inside movieclips inside movieclips..... etc.

you can check this working example at http://www.thenephilim.com.ar/guy.swf
and download it from http://www.thenephilim.com.ar/guy.fla

Expand|Select|Wrap|Line Numbers
  1. var step:Number = 8;
  2. var flipped = -guy._xscale;
  3.  
  4. onEnterFrame = function() {
  5.     if (Key.isDown(Key.RIGHT)) {
  6.         guy._xscale = -flipped;
  7.         guy._x += step;
  8.  
  9.         if(guy._currentframe < 6) {
  10.             guy.gotoAndPlay(6);
  11.         }
  12.         if(guy._currentframe < 14) {
  13.             guy.nextFrame();
  14.         } else {
  15.             guy.gotoAndPlay(6);
  16.         }
  17.     } else if (Key.isDown(Key.LEFT)) {        
  18.         guy._xscale = flipped;
  19.  
  20.         guy._x -= step;
  21.         if(guy._currentframe < 6) {
  22.             guy.gotoAndPlay(6);
  23.         }
  24.         if(guy._currentframe < 14) {
  25.             guy.nextFrame();
  26.         } else {
  27.             guy.gotoAndPlay(6);
  28.         }
  29.     } else {
  30.         guy.gotoAndStop(1);
  31.     }
  32. }
  33.  
Ok,Thanks,I'll be sure to try what you said.Then I'll send you a reply.
From,
Chinmaya
Jun 30 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Paul Nathan | last post by:
While trying to write a simple game which uses the arrow keys, I noticed that under the subroutine that handles mybase.keydown, everytime I pressed any arrow keys, or in fact any of the 9 keys...
5
by: skipc | last post by:
Hi, I am stuck... I've got a popup window that displays a list in table format with links on the bottom to navigate the list <prev> 1 2 3 ... <next> When I demo'd to the users... they...
7
by: Seash | last post by:
Hi friends , here is the sample code private void txtbox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if(e.KeyChar == 13) //enter { txtbox2.Focus(); }
4
by: Neil Wallace | last post by:
Hi there, I have an application in which a grid of 100 or more buttons are put on a form in columns of 10. All the buttons are within a panel. They are added in runtime, and so they adopt a...
7
by: matt | last post by:
Hallo, I have to create a simple sprite movement; in VB6 I used the API BitBlt and all was very good. Using the NET graphical methods (not API) , the result is slow. Do you have a NET...
1
by: Martijn Mulder | last post by:
/* I have problems detecting the Arrow Keys on a User Control. A control derived from System.Windows.Forms.Control neglects 'bare' Arrow Keys but does react on the combination <Altor <Ctrl+ Arrow...
0
by: Martijn Mulder | last post by:
/* I override IsInputKey() to direct the Arrow Keys (Cursor Keys) to my custom System.Windows.Forms.Control. But, holding down the Shift-Key prevents the Arrow Keys from coming through. How can...
2
by: Carl | last post by:
I'm new to C#, and I have only limited programming experience. I've been doing the video tutorials at MS's website, and they're very helpful, but I decided to experiment with GDI+ and have gotten...
1
by: codeman54 | last post by:
I have a picturebox that moves around using the arrow keys by using this code: If e.KeyValue = Keys.Right Then piccar.Left = piccar.Location.X + 6 ElseIf e.KeyValue = Keys.Left Then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.