ForumsProgramming ForumTwo Unknown AS2 Errors

11 4521
IQAndreas
offline
IQAndreas
299 posts
Peasant

I tried executing the following code:

public const MAXSCROLLSPEED:int = 50;
public const MINSCROLLSPEED:int = -50;


The first line received this error:
A member attribute was used incorrectly.

And the second line gave this error:
Attribute used outside class.

Could anyone help tell me what I'm doing wrong?
  • 11 Replies
jpx111
offline
jpx111
264 posts
Nomad

hmm...i just googled it, and look what I found

http://www.actionscript.org/forums/showthread.php3?t=86896

Without more of your code, I cannot help further

IQAndreas
offline
IQAndreas
299 posts
Peasant

I found that exact forum post as well, but I do not understand how the problem was finally solved.

This is the entire code:

class Background extends MovieClip
{

//IqAndreas: Limit the speed the background is allowed to scroll (otherwise it might be rediculous)
public const MAXSCROLLSPEED:int = 50;
public const MINSCROLLSPEED:int = -50; //IqAndreas: Allow the background to scroll backwards as if the ship is traveling backwards

//IqAndreas: Added a new property that allows the player to change the speed the background scrolls at.
private var _scrollSpeed:int = 1
public property get scrollSpeed():int
{
if ((newSpeed <= MAXSCROLLSPEED) && (newSpeed >= MINSCROLLSPEED)
{return _scrollSpeed;}
else //Include this just in case newSpeed is not a valid number such as null
{throw new Error("_scrollSpeed is not a valid value. Value must be between " & MINSCROLLSPEED & " and " & MAXSCROLLSPEED & ".&quot}
{
public property set scrollSpeed(newSpeed:int = 1):void
{
if (newSpeed > MAXSCROLLSPEED)
{throw new RangeError("The background speed must be less than " & MAXSCROLLSPEED & ".&quot}
else if (newSpeed < MINSCROLLSPEED)
{throw new RangeError("The background speed must be more than " & MINSCROLLSPEED & ".&quot}
else if ((newSpeed <= MAXSCROLLSPEED) && (newSpeed >= MINSCROLLSPEED)
{_scrollSpeed = newSpeed;}
else //Include this just in case newSpeed is not a valid number such as null
{throw new Error("The background speed must be between " & MINSCROLLSPEED & " and " & MAXSCROLLSPEED & ".&quot}
trace("_scrollSpeed set to",_scrollSpeed)
{
//this onEnterFrame function is a built-in function of every movie clip. It will execute all code inside it at frame rate (30 times a second)
function onEnterFrame()
{
//move the background the amount of pixels as indicated by the speed
_x -= this.scrollSpeed();
trace("Background moved",this.scrollSpeed());
//if it has travelled its entire length (2110 pixels)
//IqAndreas: THIS CODE WAS CHANGED SO THAT IF THE BACKGROUND SIZE IS NOT 2110 PIXELS, IT STILL WILL WORK
if(_x < this.width)
{
//then snap it back to zero, it will appear to scroll seamlessly
_x = 0;
}
}
}


Sorry about all the comments. I'm using someone else's code as a template, and then modifying it to make it suitable for my needs.

This file is saved as background.as and worked just fine before my changes to the code. Now the background will not scroll at all, however, the rest of the game will still run.
jpx111
offline
jpx111
264 posts
Nomad
IQAndreas
offline
IQAndreas
299 posts
Peasant

So how should I change the code?

Should I replace const with var? Or what do you mean?

IQAndreas
offline
IQAndreas
299 posts
Peasant

I tried replacing const with var and instead was met with 25 different compile errors!!

I even tried removing the public namespace, but the errors were still there.

Some of the many errors that appeared a lot were "The class or interface 'int' could not be loaded." and "A member attribute was used incorrectly."

jpx111
offline
jpx111
264 posts
Nomad

I meant that instead of using a constant, can't you just type 50 or -50 whereever it is needed? :PD

dank
offline
dank
986 posts
Peasant

This is AS3, not AS2. Make sure you're compiling in AS3 first.

All variables should be declared with 'var.' This is the proper naming convention:

attributes var:datatype[ = value];

Also, try Number in place of int. Int might give you a floating mantissa exception if you try to do certain things with it.

dank
offline
dank
986 posts
Peasant

I meant that instead of using a constant, can't you just type 50 or -50 whereever it is needed?


That's not exactly 'good' programming.
IQAndreas
offline
IQAndreas
299 posts
Peasant

This is AS3, not AS2. Make sure you're compiling in AS3 first.

That's probably the problem. The original code was in AS2, and when I added code to it, all I have learnt is AS3, so I probably used the wrong syntax in several places.

How do you change an entire FLA document from AS2 to AS3? I don't mind changing the code manually, but where do you change which compiler Flash uses for that project?

Is there any way to have the main project written in AS2, and then have some of the external AS files written in AS3?

How different is the syntax between the two? I thought they just basically added more libraries.
jpx111
offline
jpx111
264 posts
Nomad

@dank: lol...i know :P

@Iqandreas: To change it from AS3 to AS2, click on the stage and then choose Settings (beside Publish) from the property panel. And select AS2 from AS Version dropdown

dank
offline
dank
986 posts
Peasant

Your code is a mess. I have no idea if you're trying to do it in AS2 or AS3. I would scrap it and write clearly in either AS2 or AS3.

Showing 1-11 of 11