A strange way to move the player with Flash
When I look for some inspiration I browse some sites where talented designers post.
One of them is Newgrounds, almost all famous games are listed in it. I found a pretty good game called Pacco with a strange way to move the player. I have to be honest, I did not like the game itself but I liked something about the way you move the player.
I guess I can do a quite nice game starting from it.
First of all, play a bit Pacco, the follow the tutorial.
The character
The main character is a ball with a rotating arrow. Here it is how I did it: I created a movieclip linkaged as ball and inside of it I put the movieclip linkaged as arrow

The picture represents the ball movieclip, not the scene
The arrow must rotate, no matter if clockwise or counter-clockwise at the moment
-
attachMovie("ball", "ball", 1, {_x:300, _y:200});
-
ball.onEnterFrame = function() {
-
this.arrow._rotation += 5;
-
};
Line 1: crete the ball (with the arrow inside of it)
Lines 2-4: rotates the arrow of 5 degrees clockwisely at every frame
Press the mouse to stop rotation
This does not happen in Pacco but I am going to change a bit the gameplay. I want the arrow to stop when I press the mouse button
-
rotate = true;
-
attachMovie("ball", "ball", 1, {_x:300, _y:200});
-
ball.onEnterFrame = function() {
-
if (rotate) {
-
this.arrow._rotation += 5;
-
}
-
};
-
function onMouseDown() {
-
rotate = false;
-
}
Line 1: Boolean variable indicating that the arrow must rotate
Line 4: If rotate is true (and it's true by default) then proceed with the rotation routine
Lines 8-10: If the mouse button is pressed (I mean pressed, not released) the rotate variable turns to false
Now the arrow stops rotating when we press the mouse button
Charging the power
The next step is to chage the power when we hold the mouse button pressed.
I edited the ball movieclip inserting a dynamic text field called pwr... just to show the power while it charges
-
rotate = true;
-
power = 0;
-
attachMovie("ball", "ball", 1, {_x:300, _y:200});
-
ball.onEnterFrame = function() {
-
if (rotate) {
-
this.arrow._rotation += 5;
-
} else {
-
power++;
-
if (power>50) {
-
power = 50;
-
}
-
}
-
this.pwr.text = power;
-
};
-
function onMouseDown() {
-
rotate = false;
-
}
Line 2: Initializing the power to 0
Lines 7-12: If the arrow is not rotating (this means the player pressed the mouse) then start charging the power. Lines 9-11 prevents the power to raise over 50
Line 13: Displays the power writing its value in the text field
Now we have a charging power but you can notice that the power charges even if we stop holding the mouse down and there is no way to reset it.
Charging the power - step 2
Now I want the power to reset if I release the mouse button, and want the charged power to spread along arrow direction
-
rotate = true;
-
power = 0;
-
dir = 1;
-
ballspeed_x = 0;
-
ballspeed_y = 0;
-
attachMovie("ball", "ball", 1, {_x:300, _y:200});
-
ball.onEnterFrame = function() {
-
if (rotate) {
-
this.arrow._rotation += 5*dir;
-
} else {
-
power++;
-
if (power>50) {
-
power = 50;
-
}
-
}
-
this.pwr.text = power;
-
};
-
function onMouseDown() {
-
rotate = false;
-
}
-
function onMouseUp() {
-
if (!rotate) {
-
rotate = true;
-
ballspeed_x += Math.sin(ball.arrow._rotation*Math.PI/180)*power/10;
-
ballspeed_y += Math.cos(ball.arrow._rotation*Math.PI/180)*power/10;
-
power = 0;
-
dir *= -1;
-
}
-
}
Lines 4-5: Initialize ball speed at zero.
Lines 21-29: Function to call when the mouse button is released, only if rotate value is false.
Line 23: Rotate value is set to true again
Lines 24-25: ball speed x and y are calculated according to rotation and power using trigonometry as explained in this tutorial
Line 26: resets the power
Line 27: Changes the rotation way. If you noticed lines 3 and 9 you can understand how dir value affects the way of rotation. I want the rotation to change every time I release the mouse.
Now it's time to move the player
Moving the player
To have the ball moving we need this actionscript
-
rotate = true;
-
power = 0;
-
dir = 1;
-
ballspeed_x = 0;
-
ballspeed_y = 0;
-
friction = 0.99;
-
attachMovie("ball", "ball", 1, {_x:300, _y:200});
-
ball.onEnterFrame = function() {
-
if (rotate) {
-
this.arrow._rotation += 5*dir;
-
} else {
-
power++;
-
if (power>50) {
-
power = 50;
-
}
-
}
-
this._x += ballspeed_x;
-
this._y -= ballspeed_y;
-
ballspeed_x *= friction;
-
ballspeed_y *= friction;
-
this.pwr.text = power;
-
};
-
function onMouseDown() {
-
rotate = false;
-
}
-
function onMouseUp() {
-
if (!rotate) {
-
rotate = true;
-
ballspeed_x += Math.sin(ball.arrow._rotation*Math.PI/180)*power/10;
-
ballspeed_y += Math.cos(ball.arrow._rotation*Math.PI/180)*power/10;
-
power = 0;
-
dir *= -1;
-
}
-
}
Line 6: Defining the friction as we do not want the player to move forever. You can find a tutorial about friction here
Lines 17-18: Updating ball _x and _y according to ballspeed_x and ballspeed_y values
Lines 19-20: Applying the friction to ballspeed_x and ballspeed_y
Finally we have the moving ball!
Time to introduce some obstacles
The wall
The wall is the king of all obstacles. Obey the wall! I do it everyday.
I created a new movieclip linkaged as wall and changed the actionscript:
-
rotate = true;
-
power = 0;
-
dir = 1;
-
ballspeed_x = 0;
-
ballspeed_y = 0;
-
friction = 0.99;
-
attachMovie("ball", "ball", 1, {_x:300, _y:200});
-
attachMovie("wall", "wall", 2);
-
ball.onEnterFrame = function() {
-
if (rotate) {
-
this.arrow._rotation += 5*dir;
-
} else {
-
power++;
-
if (power>50) {
-
power = 50;
-
}
-
}
-
this._x += ballspeed_x;
-
this._y -= ballspeed_y;
-
ballspeed_x *= friction;
-
ballspeed_y *= friction;
-
this.pwr.text = power;
-
if (wall.hitTest(this._x, this._y-10, true) or wall.hitTest(this._x, this._y+10, true)) {
-
ballspeed_y *= -1;
-
}
-
if (wall.hitTest(this._x+10, this._y, true) or wall.hitTest(this._x-10, this._y, true)) {
-
ballspeed_x *= -1;
-
}
-
};
-
function onMouseDown() {
-
rotate = false;
-
}
-
function onMouseUp() {
-
if (!rotate) {
-
rotate = true;
-
ballspeed_x += Math.sin(ball.arrow._rotation*Math.PI/180)*power/10;
-
ballspeed_y += Math.cos(ball.arrow._rotation*Math.PI/180)*power/10;
-
power = 0;
-
dir *= -1;
-
}
-
}
Line 8: Attaching the wall in the scene
Lines 23-25: if the upper or lower part of the ball collides with the wall, I invert ball's y speed because I am sure I touched the "ceiling" or the "floor". For more information about hitTest method refer to this tutorial
Lines 26-28: Same thing with the leftmost and the rightmost part of the ball, inverting the x speed.
Caution: use this method only if you are SURE you have only horizontal or vertical straight walls. If you plan to put obstacles of different shapes or rotations, then you should apply the bounce method explained in this tutorial.
I am realizing I wrote a lot of tutorials... everytime I try to introduce something new I end writing "as said in this tutorial". This should reward regular readers increasing their knowledge without studying all tuts in a time.
Anyway, that's the game "finished". It's not over of course, as I plan to develop some new ideas starting from this tutorial.
Meanwhile, download the full sources and give me feedback.
They can be easily customized to meet the unique requirements of your project.
13 Responses to “A strange way to move the player with Flash”
Leave a Reply
Trackbacks
-
Developing a Flash game from scratch - day 1 - lobsterweb.com on
September 23rd, 2008 2:44 pm
[...] going pretty well, although I realise that this is only early days. I found a great tutorial from Emanuele Feronato which explains a neat method of moving the ball around using the mouse and a range/angle gizmo. [...]
-
A different way to move a character with Flash | Tutorial Collection on
July 1st, 2009 4:37 am
[...] View Tutorial No Comment var addthis_pub="izwan00"; BOOKMARK This entry was posted on Wednesday, July 1st, 2009 at 8:06 am and is filed under Adobe Flash Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. [...]
Posts
- Rick Triqui: my first PlayCrafter game
- Prototype of a Flash game like Meeblings
- Games for the game developers!
- The art of debugging
- How to embed a text file in Flash
- Create a Flash game in minutes with PlayCrafter
- Upgrade your Flash CS4 to 10.0.2
- Play Mazeroll, my latest Box2D game
- Triqui MochiAds Arcade plugin for WordPress Released!!
- The MochiAds funnel
- Flash game creation tutorial - part 1
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Create a flash draw game like Line Rider or others - part 1
- Create a Flash Racing Game Tutorial
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash artillery game - step 1
- Create a flash draw game like Line Rider or others - part 5
- Flash game creation tutorial – part 5.2




(4.9 out of 5) - Flash game creation tutorial – part 3




(4.86 out of 5) - Creation of a platform game with Flash – step 2




(4.84 out of 5) - Create a survival horror game in Flash tutorial – part 1




(4.82 out of 5) - Create a flash artillery game – step 1




(4.82 out of 5) - Create a Flash Racing Game Tutorial




(4.8 out of 5) - Create a flash artillery game – step 2




(4.75 out of 5) - New tile based platform engine – part 6 – ladders




(4.74 out of 5) - Flash game creation tutorial – part 2




(4.73 out of 5) - The experiment – one year later




(4.7 out of 5)



its cool!!
when you will be posting line rider’s next part??
and will you mind if I post ‘ICY BUBBLE II’?? it’s much more cooler than the first part.
I will be pleased to publish it of course…
managed to get the ball stuck in the ceiling
some interesting tutorials (when are you goint to go bact on the ball rewamped tutorials ?)
amazing as always, but how would you change it so when it reachs 50 it declines back down to 0 again?
mousey:
Define a variable at line 4:
forceDirection = 1;
then, in the “add force loop”, change to this:
if (rotate) {
this.arrow._rotation = 5*dir;
} else {
power = forceDirection;
if (power>50 || power
continue (don’t know why it cut off):
(btw – it should read “power = forceDirection”)
…
if (power>50 || power
Crap, I don’t get to finish the reply.
Okei, in plain text:
If power is bigger than 50 OR smaller than 0, multiply forceDirection with -1.
hey i would like to say can you make a tut of this ok?
http://www.wizards.com/playdnd/playdnd.asp
the game is in the walking tut
Hey,
This is a cool tutorial… everything works fine… I’m trying to build my first game in flash, its a school project, thought I could use this to make an interesting way of moving my character… the only problem right now it, it keeps getting stuck onto the walls… do you know what I mean? How can I solve that? And also, how do I make it so that the power goes up and down, back and forth? I tried using the above post, but it didn’t work… Any help at all is greatly appreciated!
Cheers!
you should make the arrow actually shape it like an arrow and make an animation of the arrow filling up with red. then you make a movie clip and give it the actions “onMouseDown = function() { _root.ball.hero.play(); }
onMouseUp = function() { _root.ball.arrow.gotoAndStop(1); }”
Hi,
I am curently working on a game with this tutorial. You state later on in the tut that if you use objects you need to read the bounce tutorial. I read it but I cant figure out what code I need use. Could someone help me out?
Thanks in advance!