Array error in actionscript 3.0 - actionscript-3

Here i tried to move card around by using the x and y axis it shows the following error:
TypeError: Error #1010: A term is undefined and has no properties.
at GamePlay/moveNext()[D:\TrainingAS3\GamePlay.as:71]
When I click the button for move cards it shows in this statement
Globe.self.realstage.TweenLite.to(anEntry['card'], .4, {
x:anEntry['x'], y:anEntry['y'], onComplete:moveNext } );
package
{
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import com.greensock.*;
import com.greensock.easing.*;
import Globe;
public class GamePlay
{
var currentEntry:int = -1;
var aList:Array =
[
{card:Globe.self.realstage.joker_mc, x:605.55, y:195.45},
{card:Globe.self.realstage.king_mc, x:323.80, y:298.45},
{card:Globe.self.realstage.queen_mc, x:45.85, y:213.95},
{card:Globe.self.realstage.a_mc, x:605.55, y:195.45},
{card:Globe.self.realstage.ten_mc, x:323.80, y:298.45},
{card:Globe.self.realstage.five_mc, x:45.85, y:213.95},
{card:Globe.self.realstage.two_mc, x:605.55, y:195.45},
{card:Globe.self.realstage.nine_mc, x:323.80, y:298.45},
{card:Globe.self.realstage.four_mc, x:45.85, y:213.95},
];
public function onClick(e:MouseEvent):void
{
// Unsubscribe to avoid the mess with second click.
Globe.self.realstage.click_mc.removeEventListener(MouseEvent.CLICK, onClick);
// Start process.
moveNext();
}
public function moveNext():void
{
currentEntry++;
// Stop the process if all the cards have been moved.
if (currentEntry >= aList.length) return;
// Get the entry.
var anEntry:Object = aList[currentEntry];
// Move the card.
trace(card);
Globe.self.realstage.TweenLite.to(anEntry['card'], .4, {
x:anEntry['x'], y:anEntry['y'], onComplete:moveNext } );
}
}
Could you please someone Elaborate this one ...

That error means that one of the following objects are null/undefined:
self.realstage.TweenLite or anEntry
Looking at those objects and seeing you are importing com.greensock.*, the issue is with TweenLite.
TweenLite is a class, which means it cannot be a property of realStage (which is how you are trying to access it).
To remedy the situation, just reference the TweenLite class directly since you've already imported it:
TweenLite.to(anEntry['card'], .4, {x:anEntry['x'], y:anEntry['y'], onComplete:moveNext } );
To further understand what's going on, you could research the difference between Static properties and methods and regular properties and methods.

Related

Frame can't move in actionscript3.0

Hi i have three class Mainintro.as, Gameplay.as, Lastscene.as, here i used this code to call another class.. Frame moving continuously not stoping...
Mainintro.as
package
{
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import com.greensock.*;
import com.greensock.easing.*;
import Globe;
public class MainIntro
{
public function MainIntro(Mc:MovieClip)
{
Globe.self.realstage = Mc;
Globe.self.realstage.intro_mc.stop();
Mc.intro_mc.play_mc.addEventListener(MouseEvent.CLICK, GameStart);
Mc.intro_mc.play_mc.buttonMode = true;
Mc.intro_mc.ins_btn.buttonMode = true;
var gameply:GamePlay = new GamePlay();
}
public function GameStart(e:MouseEvent):void
{
{
Globe.self.realstage.gotoAndPlay("game");
}
}
}
}
Here in this class file (Gameplay.as) the code occurs in the public function onClick caused by :
Globe.self.realstage.click_mc.addEventListener(MouseEvent.CLICK,onClick);
Which gives a null object reference error but I've given the correct name for
instance and then I tried to run ignore the errors then the frame moving not
stopping...
Gameplay.as
package
{
import com.greensock.*;
import com.greensock.easing.*;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import Globe;
public class GamePlay
{
var currentEntry:int = -1;
var aList:Array =
[
{card:Globe.self.realstage.joker_mc, x:605.55, y:195.45},
{card:Globe.self.realstage.king_mc, x:323.80, y:298.45},
{card:Globe.self.realstage.queen_mc, x:45.85, y:213.95},
{card:Globe.self.realstage.a_mc, x:605.55, y:195.45},
{card:Globe.self.realstage.ten_mc, x:323.80, y:298.45},
{card:Globe.self.realstage.five_mc, x:45.85, y:213.95},
{card:Globe.self.realstage.two_mc, x:605.55, y:195.45},
{card:Globe.self.realstage.nine_mc, x:323.80, y:298.45},
{card:Globe.self.realstage.four_mc, x:45.85, y:213.95},
];
public function GamePlay()
{
Globe.self.realstage.stop();
var gameply:LastScene = new LastScene();
Globe.self.realstage.click_mc.addEventListener(MouseEvent.CLICK,
onClick);
Globe.self.realstage.exit_mc.addEventListener(MouseEvent.CLICK,
lastScene);
}
public function lastScene(e:MouseEvent):void
{
Globe.self.realstage.gotoAndPlay("Scene");
}
public function onClick(e:MouseEvent):void
{
// Unsubscribe to avoid the mess with second click.
Globe.self.realstage.click_mc.removeEventListener(MouseEvent.CLICK,
onClick);
// Start process.
moveNext();
}
public function moveNext():void
{
Globe.self.realstage.currentEntry++;
// Stop the process if all the cards have been moved.
if (Globe.self.realstage.currentEntry >=
Globe.self.realstage.aList.length) return;
// Get the entry.
var anEntry:Object =
Globe.self.realstage.aList[Globe.self.realstage.currentEntry];
// Move the card.
Globe.self.realstage.TweenLite.to(Globe.self.realstage.anEntry['card'],
.4,
{x:Globe.self.realstage.anEntry['x'],y:Globe.self.realstage.anEntry['y'],
onComplete:moveNext});
}
}
}
I have used stop in the frame it is stopped now it's working good.

How do I fix the error 1120: Access of undefined property?

While sitting, watching and reading about framework, I tried it and cant get my program going.
So when I programmed I had 3 frames. One for pre loader, one for Game (no menu, just straight to game), and one last one for me to keep notes and patch note etc in.
I coded in the frame. I didnt have any extra .as files or nothing, and it all works.
Then I tried converting to having a GameControler.as and a C.as (for constant values etc), and that didn't work.
So I started over, and ended up just trying it out and ended with this code:
package {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
import flash.utils.*;
import flash.ui.*;
import Game.*;
public class GameController extends MovieClip {
private var score: Number;
public function GameController() {
// constructor code
}
public function startGame() {
score = C.score;
stage.addEventListener(Event.ENTER_FRAME, update);
}
public function scoreF(e: MouseEvent):void {
score = score + 1;
}
hitBtn.addEventListener(MouseEvent.CLICK, scoreF)
private function update(e: Event) {
score_n.text = String(score);
}
}
}
I end up with these two errors.
Line 30, Column 3 1120: Access of undefined property hitBtn.
Line 30, Column 45 1120: Access of undefined property scoreF.
What am I not understanding?
I just wanna click the button, witch is on stage, add up the score and update the on stage score.
Even though your question was answered, here is a pattern you might want to follow:
package {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
import flash.utils.*;
import flash.ui.*;
import Game.*;
public class GameController extends MovieClip {
private var hitBtn:MovieClip;
private var score: Number;
public function GameController() {
// constructor code
createChildren();
}
protected function createChildren():void {
// when it was not read from the display list
// or created in a subclass via inheritence
if (!hitBtn) {
hitBtn = getChildByName('hitBtn') as MovieClip;
if (hitBtn) {
hitBtn.addEventListener(MouseEvent.CLICK, scoreF);
} else {
trace('Child #hitBtn is not found or not a MovieClip.').
}
}
}
public function startGame() {
score = C.score;
if (stage) {
stage.addEventListener(Event.ENTER_FRAME, update);
} else {
trace("Attempt to start the game, although the controller is not added to stage.");
}
}
public function scoreF(e: MouseEvent):void {
score = score + 1;
}
private function update(e: Event) {
score_n.text = String(score);
}
}
}
When using Flash to add children, those are added to the MovieClip when it is created, so you can access them right away. Following the pattern will give you more safety when working on larger projects, which sometimes change ... this way you can get very fast an idea of what's wrong.

AS3 - How to get current scene name in class

I'm playing around with flash, and I've created multiple scenes for things like menu's, buttons, etc. When trying to add event handlers for buttons that are in one scene, but not others, the compiler complains saying that it can't reference to objects that don't exist.
I figured the solution to be simple... Get the scene name, match that against an if statement and load the event handlers through the if statements...
However, after digging around on the net for far too long, I just can't seem to find a way to do this properly. Does anyone know a way?
I've tried using the following :
var scene:Scene = myflvandclassname.currentScene;
var sName:String = MovieClip.currentScene.name;
Both lead to an error "Access of possibly undefined property Scene through a reference with static type Class".
Omit MovieClip and scenes as source for organising your project, and code on the timeline. Use Document class as entry point. This tutorial should help you to grasp main concept.
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
public class StackOverflow extends Sprite {
public function StackOverflow() {
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
setup();
}
private function setup():void {
const padding:int = 20;
//Initiate your UI components and place them in the display list
var menu:MyMenu = new MyMenu();
var buttons:MyFooterButtons = new MyFooterButtons();
var etc:AnotherComponent = new AnotherComponent();
addChild(menu);
addChild(buttons);
addChild(etc);
menu.x = menu.y = padding;
//Place them and initialise with concrete information
}
}
}
For example, MyMenu, MyFooterButtons, AnotherComponent could be MovieClip/Sprite in the library with export settings, where you did all your work with placement, styling, etc
I had a same problem. I wanted to check from an external Class the current scene name and depending on the name (name of the game level) to pass some values in some attributes… So what I did and it worked is that.
//main in 1st frame of the fla
stop();
var myCheckSceneClass: CheckSceneClass = new CheckSceneClass();
myCheckSceneClass.myCurrentScene = currentScene;
myCheckSceneClass.checkScene();
//CheckSceneClass
package {
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.Scene;
public class CheckSceneClass extends flash.display.MovieClip {
public var myCurrentScene : Scene;
public function CheckSceneClass () {
}
public function checkScene() {
switch (myCurrentScene.name) {
case "Scene 1":
trace("It is the 1st Scene");
break;
default:
trace("Error with Scene Name");
break;
}
}
}
}

Trying to remove child with mouseclick

So, my question is as follows.
Why am I getting this error
(TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/removeChild()
at TargetMain/killTarget())
when trying to remove objects from the stage with a mouse click?
My code for the application is below.
package
{
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.ui.Keyboard;
public class TargetMain extends MovieClip
{
public function TargetMain()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, spawner);//Spawning function listener
stage.addEventListener(MouseEvent.CLICK, killTarget);//Clicking function listener
}
public function spawner(k:KeyboardEvent):void
{
if(k.keyCode == 32)
{
trace ("spawned");
var theTarget:ParaspriteFull = new ParaspriteFull();
theTarget.x = Math.floor(Math.random() * stage.stageWidth);
theTarget.y = Math.floor(Math.random() * stage.stageHeight);
addChild(theTarget);
}
}
public function killTarget(toDie:MouseEvent):void
{
trace ("clicked")
var deadTarget:ParaspriteFull = (toDie.target as ParaspriteFull);
//Below is where I continually get an error and do not know how to fix it.
//This is also after searching the internet for hours trying to solve my issue.
//MovieClip(deadTarget).parent.removeChild(deadTarget);
removeChild(deadTarget);
}
}
}
Any help is greatly appreciated.
The error means deadTarget is null, so if you just want to remove deadTarget from stage, try this
var deadTarget:DisplayObject = toDie.target as DisplayObject;
if ( deadTarget && deadTarget.parent) {
deadTarget.parent.removeChild(deadTarget);
}
Or you should find out the actural type of deadTarget.
You are listening for click on stage. So, any clicks (whether it's on ParaspriteFull object or not) would fire killTarget. One way to avoid exception is to do as Pan has suggested in the answer to essentially do nothing in click killTarget if the clicked object isn't of type ParaspriteFull.
But, I'd suggest to listen for clicks on ParaspriteFull objects and not on stage. i.e. remove
stage.addEventListener(MouseEvent.CLICK, killTarget);//Clicking function listener
from your constructor and modify spawner function to add click listener as:
theTarget.addEventListener(MouseEvent.CLICK, killTarget);//Clicking function listener
Also, remove listener on ParaspriteFull object in killTarget as:
deadTarget.removeEventListener(MouseEvent.CLICK, killTarget);//Remove clicking function listener

AS 3.0: Calling a method, which is defined in another class, from Main class gives Error 1120

I have two classes. The Main class calls a function, which is defined in a Second class. I'm getting the following error:
Error 1120: Access of undefined property myFunction
Basically, I am creating buttons in the Main class that will add a corresponding Child to an Object in the Second class (if you click one button, child x1 will be added, if you click another button, child x2 will be added, and so forth).
Here's the relevant code for the Main.as file:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends MovieClip {
private var x1:X1 = new X1();
private var x2:X2 = new X2();
public function Main():void {
addPlayers();
}
public function addPlayers():void {
addChild(x1);
addChild(x2);
x1.x=325;
x1.y=5;
x2.x=366;
x2.y=5;
x1.label = "dog";
x2.label = "cat";
x1.addEventListener(MouseEvent.CLICK, selectPlayer);
x2.addEventListener(MouseEvent.CLICK, selectPlayer);
}
}
}
The Second.as file code is:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;
public class Second extends MovieClip
{
public var myVar:MyVar = new MyVar();
public function Second():void
{
addChild(myVar);
}
private var mc_x1:Mc_x1 = new Mc_x1();
private var mc_x2:Mc_x2 = new Mc_x2();
public function selectPlayer(event:MouseEvent):void
{
if (Game(this.parent).turn == 0) {
myVar.addChild(mc_x1);
} else {
switch (event.currentTarget.label) {
case "dog":
myVar.addChild(mc_x1);
break;
case "cat":
myVar.addChild(mc_x2);
break;
default:
myVar.addChild(mc_x1);
}
}
}
}
}
i've tried defining a new variable as a public static var and that hasn't worked. i've also tried to import the Second class in the Main class and that didn't work either. any thoughts?
thank you!
If I'm understanding what your code says, you can't do what your doing. Even though you are listening to events on X1 and X2, you are listening to them FROM main. In other words, main is attempting to handle the event, and it's not finding the function you specified (selectPlayer). If you want all event handling to happen in main, then main will have to call into X1 and X2 when it receives an event.
I agree with ThatSteveGuy , in Main you are calling a function that doesn't exist, namely selectPlayer(). Following your code the first thing you should do is add a selectPlayer() function in Main.
Then you would need to add an instance of the Second class in Main in order for the following to work
private function selectPlayer(event:MouseEvent):void
{
second.selectPlayer(event);
}
Now it's a bit difficult to advise you any further because this way of doing things looks a bit convoluted but then again I would need to see the big picture. This is just an explanation about why your code doesn't work. Also, in the Second class , selectPlayer may throw an error because Game(this.parent ) will return a null value so you won't be able to access the turn property...
Is there a good reason why you need to add the two buttons in Main as opposed to adding them in Second?
try this
public function Second():void
{
this.addEventListener(MouseEvent.CLICK, selectPlayer);
addChild(myVar);
}
(and don't forget to remove x1.addEventListener, x2.addEventListener from main)