PDA

View Full Version : AS3 - stage.scaleMode - TypeError: Error #1009:



valetine
November 9th, 2008, 08:08 AM
Hi!

I'm trying to prevent my swf movie to scale and align top/left. This is the code that i put on my first frame movie:



stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT


Everything works fine. Now i'm trying to make the same but using a class like this:

viewer.as:


package classes{

import flash.display.Stage;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;

public class viewer {
private var mcStage:Stage;
public function viewer():void {
init();
}

public function init():void {
mcStage.stage.scaleMode = StageScaleMode.NO_SCALE;
mcStage.stage.align = StageAlign.TOP_LEFT;
}
}
}
main.fla:


import classes.viewer;
var mystage:viewer = new viewer();
And i get this error:


TypeError: Error #1009: Cannot access a property or method of a null object reference.
at classes::viewer/init()
at classes::viewer()
at viewer_fla::MainTimeline/frame1()
What's wrong?

I understand that i'm trying to access the stage before it's ready, but i can't understand why the code doesn't work when i put in a class but works when i put direct on the fla's first frame.

I hope somebody can explain me and show me the way to fix it. I was reading all night and i find many examples like this (Action Script 3: Resize SWF on Browser resize) (http://tush.wordpress.com/2007/06/25/action-script-3-resize-swf-on-browser-resize/) that works by creating an object and passing a movieclip as parameter... but in this step i don't want to relocate the movieclip (this will be my next step :krazy:), but now i just want to understand how the stage.scaleMode works inside a class...

as3 is kind of scary, i'm frustrated :hurt:.
Thanks!

scottc
November 9th, 2008, 08:19 AM
Edit:
Your mcStage is null
Try assign it something like var mcStage:Stage =
before using it.

Edit2:
i think your looking for something like:


public class viewer extends Stage{
public function viewer():void {
init();
}
public function init():void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
//or this.stage if you prefer
}
}

or extends Sprite like the link you posted.

Edit 3:
Errrr... are you just trying to resize a sprite? or the whole stage?

valetine
November 9th, 2008, 08:42 AM
Edit:
I just want to prevent the stage to be scaled. Something like your Edit2 example (that doesn't work by the way. ArgumentError: Error #2012: viewer class cannot be instantiated. :( ).

Thanks for your help!

scottc
November 9th, 2008, 08:47 AM
ah ok... well then you need to pass the stage to the class...


var myViewer:Viewer = new Viewer(Stage);

then...


public function Viewer(sta:Stage)
{
init(sta);
//or save it to the class as a private/public var
}
public function init(sta:Stage):void
{
sta.stage.scaleMode = StageScaleMode.NO_SCALE;
}

scottc
November 9th, 2008, 08:58 AM
why make a class just to stop it, when you can just place the 2 lines on your timeline/document class and get the same result?


stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;


and your probly better off making it a static function, then you dont need to assign it.


import org.cats.Viewer;

Viewer.init(Stage);

valetine
November 9th, 2008, 08:58 AM
I can't pass the Stage to the class, i'm getting this error:



Warning: 1058: Migration issue: The property Stage is no longer supported. For more information, see DisplayObject.stage..

scottc
November 9th, 2008, 09:04 AM
oops my bad...

just pass "stage" the lower case one... "Stage" is an old fp7 property i thinks.

valetine
November 9th, 2008, 09:05 AM
why make a class just to stop it, when you can just place the 2 lines on your timeline/document class and get the same result?
ActionScript Code:

stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;




That was my first attempt and it works. I'm trying to use the class just because i'm learning AS3 and I'll add some features later, but I'm stuck with the stageScaleMode thing and i want to learn how to accomplish this task using a class.

valetine
November 9th, 2008, 09:09 AM
oops my bad...

just pass "stage" the lower case one... "Stage" is an old fp7 property i thinks.

Lol, i get a new error:


5006: An ActionScript file can not have more than one externally visible definition: classes.Viewer, classes.init

am i doing something wrong?

Viewer.as:


package classes{

import flash.display.Stage;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;

public function Viewer(sta:Stage) {
init(sta);
}
public function init(sta:Stage):void {
sta.stage.scaleMode = StageScaleMode.NO_SCALE;
}
}


viewer.fla:



import classes.Viewer;
var myViewer:Viewer = new Viewer(stage);

scottc
November 9th, 2008, 09:10 AM
import org.cats.viewer;

viewer.init(stage);

package org.cats
{
import flash.display.StageAlign;
import flash.display.StageScaleMode;

public class viewer{
public static function init(sta:stage):void
{
sta.scaleMode = StageScaleMode.NO_SCALE;
sta.align = StageAlign.TOP_LEFT;
}
}
}

Hopefully that should work. :lol:

scottc
November 9th, 2008, 09:12 AM
package classes{

import flash.display.Stage;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;

//vvv--------you forgot
//public class Viewer{

public function Viewer(sta:Stage) {
init(sta);
}
public function init(sta:Stage):void {
sta.stage.scaleMode = StageScaleMode.NO_SCALE;
}
//} <-- close here
}

valetine
November 9th, 2008, 09:20 AM
Lol i forgot the public class Viewer :blush:. Excuse me!!!! Everything works now, both of them, the class and the function. Finally i can go to sleep.

Thank you! You're so kind.

sekasi
November 9th, 2008, 04:01 PM
Just so you know scott, "stage" is the actual property of displayobjects, whereas "Stage" is the class.

So I mean if you wanna create a stage variable from your doc class it'd be var myStage:Stage = stage;

: )

scottc
November 10th, 2008, 01:38 AM
Thanks sekasi for clarifying that for me. xP

Rafael Baggio
November 10th, 2008, 09:42 AM
I don`t read all posts but, one thing that really helps when you lost the stage reference is the event ADDED_TO_STAGE.



package classes{

import flash.display.*;
import flash.events.*;

public class Viewer
{

public function Viewer()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}

public function init():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
}
}

Ranoka
November 10th, 2008, 11:26 AM
Probably good to remove the eventListener afterwards:



public function init():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.removeEventListener(Event.ADDED_TO_STAGE, init);
}