View Full Version : How to Advance From AS 1.0 to AS 2.0
Logic
12-29-2006, 11:28 AM
This is going to serve as a guide for those of you who wish to learn how to advance your coding practices to ActionScript 2.0 from ActionScript 1.0. Here are some basic principles to keep in mind:
1) AS 2.0 is object-oriented and favors properties and methods over arrays and functions.
2) Make functions universal so that they can be applied many times depending on the parameters you supply.
3) Modify existing objects (like the String object) to have new methods as you see fit.
4) Use event listeners instead of always saying instance.onEnterFrame to save processing and keep your users happy.
5) Use the var statement to control your variables.
First, let's examine an AS 1.0 unsigned function and an AS 2.0 unsigned function:
//actionscript 1.0
on (release) {
someAction();
}
//actionscript 2.0
instance.onRelease = function() {
someAction();
}
//actionscript 2.0 alternative
instance.onRelease = someAction;Why is the ActionScript 2.0 better than the ActionScript 1.0 version? The most prominent reason is that you can immediately identify which instance is being targeted by the event which makes for easier debugging and tuning. The second reason would be that you can place the AS 2.0 version in the root timeline, keeping all of your coding together and tidy for viewing and refining.
Let's address the first idea I presented now: using object-oriented code. Let's start with an example to keep you interested:
var physics:Object = new Object();Okay, we now have a physics object which we can add whatever we wish to. Why is this better than a physics array? Simply because it is associative, meaning I can access properties by name instead of index number. Tell me which is easier to understand:
//object
physics.speed = 10;
//array
physics[0] = 10;But now, let's take a look at applying this (yes, I am building something you can have fun with later):
var physics:Object = new Object();
physics.x = new Object();
physics.y = new Object();Now we have 3 objects total, 1 object with two sub-objects within it. This makes managing the physics of something much easier with separate x and y controls (and if you like 3D, z control too). Now let's add some properties:
physics.x.min = -5;
physics.x.max = 5;
physics.x.cur = 0;
physics.y.min = -5;
physics.y.max = 5;
physics.y.cur = 0;Now we have some structure to our physics object which are easily understood and easily accessed. The min property is the minimum speed at which the object moves, the max property is the maximum speed at which the object moves, and the cur property is the current speed at which the object is moving. Now let's update our object each frame so that it stays accurate:
function updatePhysics() {
if (Key.isDown(Key.UP)) {
if (physics.y.cur > physics.y.min) {
physics.y.cur--;
}
} else if (Key.isDown(Key.DOWN)) {
if (physics.y.cur < physics.y.max) {
physics.y.cur++;
}
}
if (Key.isDown(Key.LEFT)) {
if (physics.x.cur > physics.x.min) {
physics.x.cur--;
}
} else if (Key.isDown(Key.RIGHT)) {
if (physics.x.cur < physics.x.max) {
physics.x.cur++;
}
}
}
this.onEnterFrame = function() {
updatePhysics();
}
Now you're probably wondering what the hell that accomplished. The answer is that you've created a perfect control system for a 2D game which you can easily add factors too. Wanna see it working? Simply create a circle and call it 'ball_mc' and I'll show you here:
this.onEnterFrame = function() {
updatePhysics();
ball_mc._x += physics.x.cur;
ball_mc._y += physics.y.cur;
}
Now you have control of your 'ball_mc' using the arrow keys using full ActionScript 2.0. It's easy to understand, navigate, portable, and more efficient.
Notice I only went over idea number 1 from that list. I would greatly appreciate it if someone else that knows ActionScript 2.0 would help me in writing this guide. Simply reply explaining one of those ideas or a new idea that you feel is important to understand.
Jacob
12-29-2006, 11:40 AM
Hate to burst your bubble but this isnt an AS 2.0 guide, its an AS 1.0 guide. If you want to show Actionscript 2 you need to show typed variables
var physics:Object = new Object();And programming using classes:
// myBall.as
class myBall extends MovieClip
{
function myBall()
{
}
}The class programming is the real AS 2.0 stuff. In reality using MovieClip.onEnterFrame/clip.onRelease/etc programming does not in itself qualify as Actionscript 2.0. They have been available since Flash Player 6 AS 1.0 and you can publish with those settings and still use those methods.
So really, this is just a guide to using better coding in AS 1.0.
Logic
12-29-2006, 11:42 AM
Hate to burst your bubble but this isnt an AS 2.0 guide, its an AS 1.0 guide. If you want to show Actionscript 2 you need to show typed variables
var physics:Object = new Object();
Uhh, why do I need a separate class when I can just have an object? I think I missed something there. Also, you should know that variable typing in ActionScript 2.0 gets ignored by the compiler. It's there purely for development and has no impact on the client. I'll add it anyway since you requested it.
I'm not looking to blow people away here either, just advance their coding techniques to be a little more compliant with standards so that when Flash 9 is released they won't feel like they're being left behind.
Jacob
12-29-2006, 11:56 AM
Yes I am completely aware that typing is ignored at runtime (in AS 2.0 but not 3.0). It doesn't mean you shouldn't do it. If you are trying to prepare people for 3.0 you need to teach them to type variables since typing is very important in 3.0.
Classes are custom objects that have their own methods and properties. They are Objects on steroids that give you real object oriented control. As subclasses they inherit the properties of their superclass. All of this you are not going to see by instantiating generic new Objects(). I'm not dogging your tutorial, I think its definitely a good start, I'm just saying it is not AS 2.0. This is all AS 1.0 code and the sample code in this guide does not show anything that AS 1 programmers cant already use. You may wish to rename this guide or try getting some real AS 2.0 stuff in (like some of the things you mentioned: listeners, etc)
Read the flash help docs on the purpose of using classes and subclasses. It will help you understand the real power of classes, OOP and AS 2.
Logic
12-29-2006, 12:05 PM
Fine:p! I'll add a class here to make you happy but I'll do it within the script so that people don't have to create an external AS file and then import it. Most people will understand the internally defined class.
EDIT: I'll do it externally just to finish this part of the guide up.
Jacob
12-29-2006, 12:21 PM
You cant define a class internally. It has to be defined in a file path matching the class package.
Its not about making me happy. I dont need this tutorial. Its about teaching properly. If you are going to teach AS 1.0 to 2.0 conversion, teach it. If I look and see code that is claiming to be AS 2 but isnt, I'm going to say something for the sake of people reading. Many likely think this stuff is AS 2.0 because someone else was confused about it and no one corrected them.
You asked for help from people who understand 2.0 well and thats what you are getting. I'm just trying to clarify a few misunderstandings you have. Nothing personal or anything ;)
Read this (http://www.kirupa.com/developer/oop2/AS2OOPindex.htm) to see what an AS 2 OOP tutorial looks like.
Edit:
Happy?:) I know it's not personally, it's that you're the only person critiquing my coding so I have to respond somehow. I do appreciate it more than it may show so please continue. The remarks about making you happy are sarcastic and intended to make some humor of the situation.
I wish I was but no. What that is is an attempt at OOP the AS 1 way. Its a start and definitely helpful, but not AS 2.0.
See the link I gave above for an AS 2 tutorial. It wont be AS 2.0 till you start showing real classes, not psuedo prototyped classes using generic objects.
You may wish to make an 'Intro to OOP using AS 1' with this stuff then a follow up to true AS 2 OOP after they get these generic principles down.
The Brown Cow
12-29-2006, 12:34 PM
Yeah, that's an AS 1.0 class definition. I use them all the time.
I actually haven't started using AS 2.0 yet. I really should. Teach me!
Jacob
12-29-2006, 12:34 PM
Yeah, that's an AS 1.0 class definition. I use them all the time.
I actually haven't started using AS 2.0 yet. I really should. Teach me!
Read the link I gave, its pretty well done:
http://www.kirupa.com/developer/oop2/AS2OOPindex.htm
The Brown Cow
12-29-2006, 12:36 PM
Ooo, long.
Alright, reading time. Back in a while.
Logic
12-29-2006, 12:39 PM
Fine, I made a class the way that the all-knowing Kirupa says (Kirupa rules):
//ball.as
class Ball {
public var x:Object = new Object();
public var y:Object = new Object();
public var friction:Number;
x.min:Number;
x.max:Number;
x.cur:Number = 0;
y.min:Number;
y.max:Number;
y.cur:Number = 0;
function Ball(x:Number, y:Number, frc:Number):Void {
x.min = -x;
x.max = x;
y.min = -y;
y.max = y;
friction = frc;
}
function update():Void {
if (Key.isDown(Key.UP)) {
if (y.cur > y.min) {
y.cur -= friction;
}
} else if (Key.isDown(Key.DOWN)) {
if (y.cur < y.max) {
y.cur += friction;
}
}
if (Key.isDown(Key.LEFT)) {
if (x.cur > x.min) {
x.cur -= friction;
}
} else if (Key.isDown(Key.RIGHT)) {
if (x.cur < x.max) {
x.cur += friction;
}
}
}
}Now you can specify parameters and create new objects using the methods I showed you before.
Jared
12-29-2006, 12:42 PM
Ya the title can be misleading, some people might actual think its AS 2. Probably should change it as a introduction to OOP in AS 1. And than go onto AS 2 once they get the AS 1 down. Sense starting out with AS 2 might be confusing for starters. Still classes are pretty easy to use. I never did like the prototypes for AS 1 at all.
LizardRob
12-29-2006, 12:56 PM
...
I hardly understand a word in this thread...
I'm never leaving what I though was AS 2.0 now...
Logic
12-29-2006, 01:04 PM
Wow I never realized ActionScript 2.0 was so similar to Java with classes (I read the whole Kirupa article). Thanks for sharing that with me JJ; it'll make development for me much easier.
Jacob
12-29-2006, 01:07 PM
Fine, I made a class the way that the all-knowing Kirupa says (Kirupa rules):
//ball.as
class Ball {
public var x:Object = new Object();
public var y:Object = new Object();
public var friction:Number;
x.min:Number;
x.max:Number;
x.cur:Number = 0;
y.min:Number;
y.max:Number;
y.cur:Number = 0;
function Ball(x:Number, y:Number, frc:Number):Void {
x.min = -x;
x.max = x;
y.min = -y;
y.max = y;
friction = frc;
}
function update():Void {
if (Key.isDown(Key.UP)) {
if (y.cur > y.min) {
y.cur -= friction;
}
} else if (Key.isDown(Key.DOWN)) {
if (y.cur < y.max) {
y.cur += friction;
}
}
if (Key.isDown(Key.LEFT)) {
if (x.cur > x.min) {
x.cur -= friction;
}
} else if (Key.isDown(Key.RIGHT)) {
if (x.cur < x.max) {
x.cur += friction;
}
}
}
}Now you can specify parameters and create new objects using the methods I showed you before.
Did you even try compiling this? Its not valid code. I suggest you change your x/y min max values to local variables or else use a vector class instead of generic objects.
In fact if you have Flash 8 you can probably just use the built in Point class
import flash.geom.Point;
var myPoint:Point = new Point();
Wow I never realized ActionScript 2.0 was so similar to Java with classes (I read the whole Kirupa article). Thanks for sharing that with me JJ; it'll make development for me much easier.
Yes, and AS 3.0 is even more similiar and compliant.
Logic
12-29-2006, 01:14 PM
Did you even try compiling this?
Nope:). I have to play around now in Flash for a little while until I get a feel for the "new" class system (new because I didn't know it before). I'll post back here when I get the hang of it completely.
Jacob
12-29-2006, 01:28 PM
Ok. Let me know if you have questions.
LizardRob
12-29-2006, 01:35 PM
Where am I? I'm lost.
Malignus
12-29-2006, 04:08 PM
I've already decided that my next game is going to be coded in AS 2.0. I'm taking baby steps here. :D
magcius
12-30-2006, 08:36 AM
Here, Logic. Check this one out.
I wrote it a long time ago, it isn't very well written, but it is a proper guide to AS2.
http://forums.xgenstudios.com/showthread.php?t=74159&highlight=starter%27s+guide%5C
Logic
12-30-2006, 08:07 PM
Thanks magcius, looks consistent with the Kirupa guide and does a good job explaining things differently. I just wrote my first class file (finally got my comp working again with Flash - part of the reason I didn't test that class before posting it) and I must say that I really love it. It makes keeping track of things much easier and gives you a lot of control. Tell me what you guys think of it:
//class stat for stat.as
//used to get stats about the SWF
class stat {
//vars
private var oldTime:Number;
private var curTime:Number;
//constructor
function stat() {
}
//get delta
function getDelta():Number {
oldTime = curTime;
curTime = getTimer();
return curTime-oldTime;
}
//get fps
function getFps():Number {
return Math.round(1000/getDelta()*4);
}
}Comments? Improvements I could make? Yay?
EDIT: Hey JJ, turns out you can define classes internally. I decompiled the ActionScript from the testing SWF using Flare to just what Flash does when it imports an external class. Check it out:
movieClip 1 __Packages.stat {
#initclip
if (!_global.stat) {
var v1 = function () {};
_global.stat = v1;
var v2 = v1.prototype;
v2.getDelta = function () {
this.oldTime = this.curTime;
this.curTime = getTimer();
return this.curTime - this.oldTime;
};
v2.getFps = function () {
return Math.round((1000 / this.getDelta()) * 4);
};
ASSetPropFlags(_global.stat.prototype, null, 1);
}
#endinitclip
}
}Just thought you might like to see that. I think it's interesting to see how it imports.
The Brown Cow
12-30-2006, 08:24 PM
Yep, AS 2.0 compiles into AS 1.0. Interesting to see how it does it.
Jacob
12-30-2006, 09:09 PM
EDIT: Hey JJ, turns out you can define classes internally. I decompiled the ActionScript from the testing SWF using Flare to just what Flash does when it imports an external class. Check it out:
Using hacked together code, low level AS methods and bad coding practices. This is because (as TBC stated) AS 2.0 compiles down using the AVM1 machine. This however, is only a behind the scenes process and is not how anyone should code (or is intended to code). It is also why AS 3.0 has a new virtual machine (AVM2) and uses that new virtual machine to run more properly compiled code, instead of attempting to compile down.
In some instances the ASSetPropFlags method is useful, but in short, dont use in an attempt to define a class internally unless absolutely necessary (which is pretty much never).
Jonanin
12-30-2006, 09:14 PM
Man, I really need to get started in AS2.0, like TBC. I'll be reading that also!
As far as AS3 goes, I don't think I want to get into that yet. Oh well. This shouldn't be too hard, I am very familiar with OOP from working with C#.
Jared
12-30-2006, 09:47 PM
Using hacked together code, low level AS methods and bad coding practices. This is because (as TBC stated) AS 2.0 compiles down using the AVM1 machine. This however, is only a behind the scenes process and is not how anyone should code (or is intended to code). It is also why AS 3.0 has a new virtual machine (AVM2) and uses that new virtual machine to run more properly compiled code, instead of attempting to compile down.
In some instances the ASSetPropFlags method is useful, but in short, dont use in an attempt to define a class internally unless absolutely necessary (which is pretty much never).
Wanna talk about low level coding try writing any good sized programs in a x86 assembly. Pure evil is how one can best describe that language.
arkhan
12-31-2006, 08:48 AM
just wanted to note that stict typing do make a diference in AS2..
and NO ONE should ever have to see assembly..
also, even tho it can be considered AS1 as said, I prefer to use objects for simple things, and only define classes for bigger things..if the code gets loose all around classes and subclasses the whole purpose of using AS2 is lost so..
evn using other IDEs that makes it much much easyer to play around with .as files, its anoying to go back and forth with 50 very small classes when the debugger cant locate the very source of a bug very well..
Logic
12-31-2006, 08:53 AM
just wanted to note that stict typing do make a diference in AS2.
The only difference it makes is when you're scoping variables with the 'var' statement. All other typing gets ignored from what I've seen from the bytecode or at least I thought it did.
arkhan
12-31-2006, 10:45 AM
var typing and returned method values are considered, and it do return errors when compiling/runtime..
a simple test for it
var num:Number = new Number(0);
num = "Hello!";
function returnBool():Boolean
{
return 123
};
this proves that strict typing in AS2 do make a diference..
it cant be declared without the var tho
num:Number = new Number();
this gives you an error, so, the only way to make any strict typing is with the var statement..
---edit--
also, I forgot the arguments..they do mater as well..
function alo(i:Number,j:String):Void
{
};
alo(1,2);
magcius
12-31-2006, 11:00 AM
Number 1. Roller Coaster Tycoon (1 & 2) was almost coded entirely in x86 assembly by Chris Sawyer.
Number 2. Logic, typing errors happen in the compiler, as with every other language. The flash compiler is horrible though. Try looking at Flash v2 components source code to see how they exploit the compiler to their benefit. What's increasingly bad is that there are almost no runtime errors set by default, you have to throw your own with the "throw" and "try...catch...finally" statements.
Exploits in the flash compiler, I repeat DO NOT USE THIS!:
class SomeClass {
var variable1:Number;
var variable2:String;
function SomeClass () {
}
}
var exploitTest:SomeClass = new SomeClass ();
exploitTest.variable1 = 11;
exploitTest.variable2 = "Foo";
exploitTest.variable3 = true; // Throws error, no variable defined.
exploitTest["variable3"] = true; // No error thrown, compiler mistake.
Jacob
12-31-2006, 11:20 AM
The only difference it makes is when you're scoping variables with the 'var' statement. All other typing gets ignored from what I've seen from the bytecode or at least I thought it did.
In Class OOP you have to define var in front of variables. Strict typing in AS 2.0 is used to helped improve coding as well as compile time debugging. Its just like the fact that if you dont define a variable in a class and try to use it it will throw an error.
In AS 3.0 (as I've said) it goes beyond that.
Exploits in the flash compiler, I repeat DO NOT USE THIS!:
Using code like
exploitTest["variable3"] is often necessary for coding. The practice in itself is not bad and is a standard practice, its just easier to screw up. Additionally, in AS 3.0 if the property doesn't exist the compiler will throw a reference error:
ReferenceError: Error #1056: Cannot create property variable3 on SomeClass.
at SomeClassFile_fla::MainTimeline/SomeClassFile_fla::frame1()
The Brown Cow
12-31-2006, 11:52 AM
var typing and returned method values are considered, and it do return errors when compiling/runtime..
a simple test for it
var num:Number = new Number(0);
num = "Hello!";
function returnBool():Boolean
{
return 123
};
this proves that strict typing in AS2 do make a diference..
Ah, but those are compile-time errors. After being compiled (into AS 1.0), the strict typing no longer makes a difference.
Jacob
12-31-2006, 12:31 PM
Ah, but those are compile-time errors. After being compiled (into AS 1.0), the strict typing no longer makes a difference.
Which is why in AS 2.0 it primarily a development aid and they should still use it. And it does aid development a lot when used as intended. The first AS 2.0 game I did in was for Disney and the bug testing cycle was cut down by an incredible amount simply because of better coding practice using strict typing and better OOP.
AS 3.0 has it as a development aid for coding & compile time checking like now as well as runtime usage.
The Brown Cow
12-31-2006, 01:07 PM
I'm not really going to bother with AS 2.0. I'm pretty content with class structures in AS 1.0 and don't really have big debug issues. I'm not working with a group here, so I don't really have to worry about modularity and all that good stuff.
I will definitely learn AS 3.0 though, as it offers significant processing advantages and other good things.
Jacob
12-31-2006, 06:44 PM
I would shoot myself if I had to code inside the fla with AS 1.0. AS 2.0 is awesome when you become comfortable with it. There is a reason good programmers use it.
Freddy
01-03-2007, 09:22 PM
Sorry to completely ignroe the current conversation for a second, but I really didn't feel like starting a new thread seeing as this has to do with Objects and the first post mentioned it a lot.
Here goes.
I have and object and a number variable called 'myObj' and 'myNum', respectfully.
If I had something like this:
for(myNum = 0; myNum < 6; myNum++){
myObj.myNum = myNum + 5;
trace(myObj.myNum + "____" + myNum);
}
would I get:
5____0
6____1
7____2
8____3
9____4
10____5
basically what I'm asking is, can use varialbes in objects to get a bunch of different values? or is that even valid?
Homakruz
01-03-2007, 09:44 PM
Yeah you can, I do it all the time. Is it some as2 style though cause it I have just been winging it and wouldn't know lol.
Jared
01-03-2007, 09:46 PM
First you have to make the object before you can assign a property to it like this which would give you those results. But you have to remember for which loop you go through what you are doing is overwriting the myNum property with a new value.
var myObj:Object = new Object();
for(myNum = 0; myNum < 6; myNum++){
myObj.myNum = myNum + 5;
trace(myObj.myNum + "____" + myNum);
}
Jacob
01-03-2007, 09:50 PM
To set or access variables/properties in other objects use the brackets [] and put a string/value between them:
var myObject:Object = new Object();
var myNum:Number = 5;
myObject["myProperty"] = 6;
trace( myObject["myProperty"] ); // 6
myObject["var"+myNum] = "something";
trace( myObject["var"+myNum]); // something
trace( myObject["var5"] ); // something
Freddy
01-03-2007, 09:54 PM
ahh!
let me try it then
thanx
Jared
01-03-2007, 10:01 PM
To set or access variables/properties in other objects use the brackets [] and put a string/value between them:
var myObject:Object = new Object();
var myNum:Number = 5;
myObject["myProperty"] = 6;
trace( myObject["myProperty"] ); // 6
myObject["var"+myNum] = "something";
trace( myObject["var"+myNum]); // something
trace( myObject["var5"] ); // something
But you're not even using the myNum why did you define it. You know something like this
var myObject:Object = new Object();
var myNum:Number = 5;
myObject.num = myNum;
myObject.sm = "something";
trace(myObject.num);
trace(myObject.sm);
Jacob
01-03-2007, 10:05 PM
But you're not even using the myNum why did you define it:confused:
Yes I am. Put on your glasses and read again :D.
Shadow, on the same line of thought, heres another example:
var obj1:Object = new Object();
var obj2:Object = new Object();
obj1.myNum = 5;
obj2["var"+obj1.myNum] = "something";
trace( obj2["var"+obj1.myNum]); // something
Jared
01-03-2007, 10:14 PM
Yes I am. Put on your glasses and read again :D.
Shadow, on the same line of thought, heres another example:
var obj1:Object = new Object();
var obj2:Object = new Object();
obj1.myNum = 5;
obj2["var"+obj1.myNum] = "something";
trace( obj2["var"+obj1.myNum]); // something
Oh I get what you're saying I guess I ment using that number to set a property to but now I see you where using that to set the property itself my bad.
Jacob
01-03-2007, 10:35 PM
Yeah. No worries.
Jared
01-03-2007, 10:47 PM
Heres another example. This time using a constructor function.
var myNum:Number = 5;
function Something(Num, Str) {
this.num = Num;
this.str = Str;
}
var myobj:Object = new Something(myNum, 'something');
trace(myobj.num); // 5
trace(myobj.str); // something
Freddy
01-04-2007, 12:46 PM
it still seems to come up undefined. what am I doing wrong?
function onMouseDown(){
fire = _root.attachMovie("bullet", "shot"+shotCount, shotCount);
fire._x = _root.hero._x;
fire._y = _root.hero._y;
if(_root._ymouse <= _root.hero._y){
fireAngle["shotCount"] = degHero;
}else{
fireAngle["shotCount"] = degHero + 180;
}
fire._rotation = fireAngle["shotCount"];
trace(fireAngle["shotCount"]);
shotCount++;
}
fireAngle has already been defined as an object and shotCount has been defined as a number and given the value of zero prior to the function.
I'm confused.
Jared
01-04-2007, 01:17 PM
I don't see degHero variable defined anywhere nor are you making the object before assigning the property to it like var fireAngle:Object = new Object();
Freddy
01-04-2007, 01:23 PM
they are both dedined previously. and 'var fireAngle:Object = new Object();' is exactly what I have for it.
degHero has value cause I traced it to make sure. It's the angle of another clip.
Jared
01-04-2007, 01:28 PM
Have you traced the if else statements to see if either one of them are being activated?
Freddy
01-04-2007, 01:31 PM
good idea. I'll do that now
yup, both of them.
Jared
01-04-2007, 01:36 PM
Impossible to tell than just by seeing that code.
arkhan
01-04-2007, 07:54 PM
what do you mean "both"? I see only one if..
and are you sure you defined shotCount inside fireAngle?
if new Object(); is what you used, its possible
and the only possibility for it to return undefined would be if deghero was undefined...make sure you are in the right scope for it..
Freddy
01-04-2007, 09:05 PM
well I'll try to post everything relevant.
first scene of the game. where I define everything for later use:
var shotCount:Number = new Number;
var fireAngle:Object = new Object;
if a seperate function, I convert radians to degrees to get 'degHero':
_root.degHero = radHero * (180/Math.PI);
note: 'radHero' is not undefined. degHero does have a value I know because antoher clip is using it and it works for that clip.
then of course what I posted in a previous post. basicaly I'm attaching a clip to a certain spot and I want it to rotate in the same angle of another clip. that angle is 'degHero'.
function onMouseDown(){
fire = _root.attachMovie("bullet", "shot"+shotCount, shotCount);
fire._x = _root.hero._x;
fire._y = _root.hero._y;
if(_root._ymouse <= _root.hero._y){
fireAngle["shotCount"] = degHero;
trace(fireAngle["shotCount"]);
}else{
fireAngle["shotCount"] = degHero + 180;
trace(fireAngle["shotCount"]);
}
fire._rotation = fireAngle["shotCount"];
shotCount++;
}
Jacob
01-04-2007, 09:14 PM
I didnt read the rest but I can tell you right off that
var fireAngle:Object = new Object;needs to be
var fireAngle:Object = new Object();Dont know if thats your problem cause I dont have time to check it all out
Additionally you dont need to put
var shotCount:Number = new Number;This is sufficient:
var shotCount:Number;
Jared
01-04-2007, 09:22 PM
And where are you getting the radHero value from?
Also putting in () at the end of the new object is not required, at least not in flash8 and won't break the code. You perfectly well exclude it and it'll make the object just fine. Though in the spirit of good coding practices you should.
Freddy
01-04-2007, 09:50 PM
well I fixed the part where I defined my stuff. even though I guess it didn't matter, I did it anyway.
also radHero comes from a previous functioin that takes the tangent of the subtracted _xmouse value from the _x of another clip and the subtracted _ymouse value from the _y of another clip.
I just got an angle in radians. did it dozens of times in the past. this isn't a problem.
if you must know, here it is.
_root.xDistHero = _root.hero._x - _root._xmouse;
_root.yDistHero = -(_root.hero._y - _root._ymouse);
_root.radHero = Math.atan(xDistHero/yDistHero);
Jared
01-04-2007, 09:54 PM
So what exactly is you're problem? That it's not defining the shotCount property in the fireAngle object? Cause I would need a working version of the code in action to determine why it would be doing that.
Freddy
01-04-2007, 10:02 PM
well then seeing as I'm out of alternatives, would you mind if you PMed me your email address so I can send you the .fla.
It's really annoying since I was hoping to not have to show this to anyone for quite some time. But I've no choice.
Jared
01-04-2007, 10:15 PM
You can upload it to http://www.mytempdir.com and pm me in the link if you want. Also I only require a fla with the code in question in action to determine the problem. It's not like I need the fla for the entire project or whatever you're working on.
arkhan
01-05-2007, 09:02 AM
are you sure you defined shotCount inside fireAngle?
no..shotCount is obviously outside fireAngle..
try to trace degHero instead of fireAngle["shotCount"]..you might be in the wrong scope..
if shotCount is a number, you cant define a var fireAngle[shotCount]..because var names cant start with numbers..
Jared
01-05-2007, 09:22 AM
It turned out the problem was he was publishing it for AS 1.0 and that coding method only works when publishing for AS 2.0.
I guess we logically assumed he was publishing it for AS 2.0 to begin with.
Freddy
01-05-2007, 12:55 PM
no..shotCount is obviously outside fireAngle..
try to trace degHero instead of fireAngle["shotCount"]..you might be in the wrong scope..
if shotCount is a number, you cant define a var fireAngle[shotCount]..because var names cant start with numbers..
I see.
so then it can easily changed so that it doesn't start with a number. at least that part is solved.
now about this AS 1.0 and AS 2.0 stuff.
I could have sworn objects were around since like flash 5. why cant I use them with AS 1.0?
Jacob
01-05-2007, 01:08 PM
They have been around awhile and can be used in AS 1.0. But its likely some things in your code (like variable typing) were interfering and breaking the code since they are for AS 2.0.
Jared
01-05-2007, 01:11 PM
That is correct jjcorreia variable typing such as var fireAngle:Object = new Object();
Results in broken code when published for AS 1.0 The solution is a easy one take off the :Object part and the problem is solved. Also you'll want to take off all variable typing, so no other problems arise.
Freddy
01-05-2007, 01:17 PM
score!
it worked. thanx!
and I'm sorry for turning this thread into a "lets help shadow" thread.
Jacob
01-05-2007, 02:47 PM
No problem shadow, thats what this forum is for.
vBulletin® v3.7.3, Copyright ©2000-2010, Jelsoft Enterprises Ltd.