-
Senior Member
[RESOLVED] Can't set array values inside a class
This is problably a noob question, sorry.
I have this code inside a public class:
Code:
// background
const ww = 480;
const hh = 272;
// data for the target
var targDisp:int = 25;
var targWidth:int = 50;
var targHeight:int = 50;
var targMargin:int = 10;
// Define positions
var targPositions:Array = new Array();
targPositions[0] = [(ww / 2) - (targWidth / 2),targMargin];// upper mid position
targPositions[1] = [(ww / 2) - (targWidth / 2),targHeight - targMargin];// lower mid position
targPositions[2] = [targMargin,(hh / 2) - (targHeight / 2)];// left
targPositions[3] = [targWidth - targMargin,(hh / 2) - (targHeight / 2)];// right
When running the code I get:
Code:
Line 51 1120: Access of undefined property targPositions.
And so on. I get no error for declaring the targPositions aray, however get lots of error when setting values to it.
If I place the code in the main timeline it works fine.
-
Senior Member
I don't get any error:
PHP Code:
package { import flash.display.Sprite; public class Arraytest extends Sprite { public function Arraytest() { // background const ww = 480; const hh = 272;
// data for the target var targDisp:int = 25; var targWidth:int = 50; var targHeight:int = 50; var targMargin:int = 10;
// Define positions var targPositions:Array = new Array();
targPositions[0] = [(ww / 2) - (targWidth / 2),targMargin];// upper mid position targPositions[1] = [(ww / 2) - (targWidth / 2),targHeight - targMargin];// lower mid position targPositions[2] = [targMargin,(hh / 2) - (targHeight / 2)];// left targPositions[3] = [targWidth - targMargin,(hh / 2) - (targHeight / 2)];// right
trace(targPositions[0]); } } }
Arraytest is the Document class here.
- The right of the People to create Flash movies shall not be infringed. -
-
Senior Member
Ooops, I had placed the code right under the class. Why can I define variables there, but not arrays?
PHP Code:
package { import flash.display.Sprite; public class Arraytest extends Sprite { // background const ww = 480; const hh = 272;
// data for the target var targDisp:int = 25; var targWidth:int = 50; var targHeight:int = 50; var targMargin:int = 10;
// Define positions var targPositions:Array = new Array();
targPositions[0] = [(ww / 2) - (targWidth / 2),targMargin];// upper mid position targPositions[1] = [(ww / 2) - (targWidth / 2),targHeight - targMargin];// lower mid position targPositions[2] = [targMargin,(hh / 2) - (targHeight / 2)];// left targPositions[3] = [targWidth - targMargin,(hh / 2) - (targHeight / 2)];// right
public function Arraytest() { trace (targPositions[0][0]) } } }
-
Senior Member
You need to place the execution within the function frame:
public function Arraytest()
{
targPositions[0] = [(ww / 2) - (targWidth / 2),targMargin];// upper mid position
targPositions[1] = [(ww / 2) - (targWidth / 2),targHeight - targMargin];// lower mid position
targPositions[2] = [targMargin,(hh / 2) - (targHeight / 2)];// left
targPositions[3] = [targWidth - targMargin,(hh / 2) - (targHeight / 2)];// right
trace(targPositions[0][0]);
}
Also you should define your variables as public or private and not let the compiler guess it, for example
private var targMargin:int = 10;
- The right of the People to create Flash movies shall not be infringed. -
-
Senior Member
Thank you very much!
P.s: the link to your book in the footer signature does not seem to work.
-
Senior Member
Thanks
- The right of the People to create Flash movies shall not be infringed. -
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
|