PDA

View Full Version : ball game question


drumn4life0789
03-10-2008, 08:52 AM
Hello everyone. I am trying to create a little ball game right now in flash. I am kind of new to flash but have been looking at it for a while now that I am starting to understand the actionscript and things now.

Basically I am making a ball game where the ball has gravity on it. you move it with the arrow keys and such, it has gravity and all that. The point of the game is to get from point A to point B.

Well the main problem I have right now in my code is that when i move my ball which i have named hero_mc in my code it is also moving my walls which I am going to set up a hittest function later.

here is my code

//---------------------------------------VARIABLES-----------------------------------\\
var power:Number = 0.65;
var xspeed:Number = 0;
var yspeed:Number = 0;
var friction:Number = .99;
var gravity:Number = 0.2;
var thrust:Number = .75;
//---------------------------------------VARIABLES-----------------------------------\\

//---------------------------------------FUNCTIONS-----------------------------------\\
function movement () {
if (Key.isDown(Key.LEFT)) {
xspeed-=power;
}
if (Key.isDown(Key.RIGHT)) {
xspeed+=power;
}
if (Key.isDown(Key.UP)) {
yspeed-=power*thrust;
}
if (Key.isDown(Key.DOWN)) {
yspeed+=power*thrust;
}
xspeed *= friction;
yspeed += gravity;
_x += xspeed;
_y += yspeed;
}

//---------------------------------------FUNCTIONS-----------------------------------\\

hero_mc.onEnterFrame = function () {
movement();
}


I dont see why the hero on enterframe code is not only working for the hero. why is it moving my walls as well.

And I have all this placed in the first frame of the file. I like to keep all my actionscript in the maintimeline of the movie and not in alternate movie clip timelines. So any help would be most appreciated.

ASWC
03-10-2008, 01:55 PM
I guess since you don't give any MC to move in your function, flash assumes you want to move the whole stage:
_x += xspeed;
_y += yspeed;
What you are trying to do is that:
function movement (target_mc:Movieclip)
then in the function:
target_mc._x += xspeed;
target_mc._y += yspeed;
then in the call:
movement(this);//you use "this" since it's in the hero_mc scope

drumn4life0789
03-11-2008, 05:42 AM
Hey thanks, I knew it was something easy that i was just overlooking. Ill try it in the morning when i get on my comp that has flash on it. thanks again for the help

Durnus
03-11-2008, 08:55 PM
Another way (simpler in my opinion, but doesn't have quite the flexibility of ASWC's solution):

Instead of doing all the target stuff use hero_mc directly in the event function.
For example:

if (Key.isDown(Key.LEFT)) {
xspeed-=power;
}
if (Key.isDown(Key.RIGHT)) {
xspeed+=power;
}
if (Key.isDown(Key.UP)) {
yspeed-=power*thrust;
}
if (Key.isDown(Key.DOWN)) {
yspeed+=power*thrust;
}
speed *= friction;
yspeed += gravity;
hero_mc._x += xspeed;
hero_mc._y += yspeed;

Also, a tip for your coding:
Movie clips can hold any random variable that you want to give it. So you could, instead of using global variables for your hero, put the xspeed INSIDE the hero.
:

hero_mc.xspeed = 0;
//instead of
var xspeed = 0;

:D

drumn4life0789
03-11-2008, 09:55 PM
ok i got the hero to move as it is supposed to.

Now I have a simple problem but I dont see why it doesnt work.

I have walls that the hero cannot touch or it will be moved back to the beginning where it started off. heres the code.

function collisionTest (hero_mc:MovieClip) {
for (var i:Number = 1; i<=4; i++) {
if (hero_mc.hitTest["wall"+i+"_mc"]) {
hero_mc._x = 100;
hero_mc._y = 100;
xspeed = 0
yspeed = 0
}
}
}
//---------------------------------------FUNCTIONS-----------------------------------\\

hero_mc.onEnterFrame = function () {
movement(this);
collisionTest(this);
}

I have the walls names wall1_mc, wall2_mc, etc. This way I can add barriers where ever i want and it wont be able to touch them.

The part where the for loop is not working right.

I can change it and just add in the individual walls and it will work like

if (hero_mc.hitTest[wall1_mc]) {

this code works but the for loop doesnt for some reason, any help will be greatly appreciated.

ASWC
03-11-2008, 11:38 PM
if (hero_mc.hitTest["wall"+i+"_mc"])
this line is wrong.Here is the right way:
if (hero_mc.hitTest(this["wall"+i+"_mc"]))

drumn4life0789
03-12-2008, 05:11 AM
thanx ASWC, I seem to not put the this in alot of my code and it seems to turn out to be the problem. Thanx again