|
-
Animated Ball Tutorial AS3 - Assistance
Hello Friends,
I wonder if someone might be able to spot what I am doing wrong in my code.
I am trying to follow and create a tutorial which explains how to make a bouncing ball.
I am really struggling with the Action Script 3.0 even though the teacher types it in.
I am inserting the code so that you can view it.
Many thanks in advance if anyone has an idea where I might be going wrong.
I am very grateful for any and all advice.
The tutorial is located:
http://www.youtube.com/watch?v=36BIN9OKNf8
My code is:
var gravity:Number = 3
var speed:Number = 0
var position:Number
var num:Number =350
addEventListener (Event.ENTER_FRAME, animation)
function animation(e:Event) : void {
speed+=gravity
position = ball.y+speed
if (position>num) (
ball.y = num
);speed = - speed=0.8;
} else {
ball.y += speed;
}
-
Prid - Outing
Several mistakes:
Actionscript Code:
var gravity:Number = 3 var speed:Number = 0 var position:Number var num:Number =350
addEventListener (Event.ENTER_FRAME, animation) function animation(e:Event) : void { speed+=gravity; position = ball.y+speed; if (ball.y>num){ ball.y = num; speed = -speed*0.8; } else { ball.y += speed; } }
I am back, guys ... and finally 18 :P
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS
-
Hello Nig 13,
Thank you very kindly. You have helped me out again!
I am trying to learn as I go along but I have to thank you and the others on FLASHKIT Support who really help those of us with the learning.
I truly appreciate all the assistance very much.
Thank you again for your help, your time and your expertise!
-
Prid - Outing
No problem, but allow me to point out the errors to help you:
- you should always have semi-colon at the end of a code line to signify its end, like this:
Actionscript Code:
var gravity:Number = 3; var speed:Number = 0; var position:Number; var num:Number =350;
this is crucial as it can sometimes cause errors without you knowing it, but it basically tells the code to stop there and not mix with anything in the lines below, because something like this works:
Actionscript Code:
var myVar = 5;
trace(myVar);
The code isn't stopped in the first line, but continues in the second line and sets its value as 5, and then the semi-colon stops it, but you don't want this to give you errors, right?
- if statement looks like this,
Actionscript Code:
if(condition){ // do stuff }
with curly brackets {} and not simple brackets ()
- always tidy your code to make it easier for yourself to spot the errors easily. This is a good example of a bad structure:
Actionscript Code:
function animation(e:Event) : void { speed+=gravity position = ball.y+speed if (position>num) ( ball.y = num );speed = - speed=0.8; } else { ball.y += speed; }
it's really hard to tell what's what, and what's happening and stuff like that. Remember that anything, a function for instance, which starts with { needs to be closed with a }, and I'd tidy the code like this:
Actionscript Code:
function animation(e:Event) : void { speed += gravity; position = ball.y+speed; if (position>num) { ball.y = num; // I have no idea what the code below does, 'cause it's wrong in every possible way, you can't have two equals signs like that speed = -speed=0.8; } else { ball.y += speed; } }
now I know where the if statement ends, and where the function ends to easily distinguish between them, and the variables
Hope this helps
I am back, guys ... and finally 18 :P
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|