Error #2025: ActionScript problems with programing - actionscript-3

The problem is that every time I try to go on to the next scene backStory
it doesn't remove the child. I want to make it so that when the counter goes to
4 it will take it out and put in a new scene. Is there a way to fix this issue? I don't understand how I can switch scenes
I am creating a game and I get this error :
ArgumentError: Error #2025: The supplied DisplayObject must be a
child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at Runner/onEnterFrame()
package {
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;
public class Runner extends MovieClip {
public var startPage:StartPage = new StartPage();
public var backStory1:BackStory1 = new BackStory1();
public var water:Water = new Water();
public function Runner() {
addChild(startPage);
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
function onEnterFrame(event:Event):void{
if(startPage.endStartPage == true){
removeChild(startPage);
addChild(backStory1);
startPage.endStartPage == false;
}
if(backStory1.backStory1End == true){ //in backstory1 the bool backstory1end is suppose to be true but it doesnt get to that point
removeChild(backStory1);
addChild(water);
startPage.endStartPage == false;
}
}
}
}
Backstory class
package {
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;
public class BackStory1 extends MovieClip {
var backStory1End:Boolean = false;
var count:int = 0;
public function BackStory1() {
backStory1Text.text = "sssherro";
if (stage)
{
init(null);
}
else
{
addEventListener(Event.ADDED_TO_STAGE,init);
}
function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
nextButton.addEventListener(MouseEvent.MOUSE_DOWN,onButtonClick);
}
function onButtonClick(event:MouseEvent):void{
count++;
if(count == 1){
backStory1Text.text = "awesome1";
//backStory1End= true;
}
else if(count == 2){
backStory1Text.text = "awesome2";
}
else if(count == 3){
backStory1Text.text = "awesome3 leave game press";
}
else if(count == 4){
//backStory1Text.text = (String)(counter);
backStory1End = true;
}
}
}
}
}

Following line is wrong.
startPage.endStartPage == false;
Fix:
startPage.endStartPage = false;

Related

AS3 - preventDefault() doesn't work in fullscreen (AIR)

I am trying to make a fullscreen program in air that does not become windowed when you press escape. Why is my program not working as it should, and how should make it work correct?
Code:
package
{
import flash.display.*;
import flash.events.*;
import flash.desktop.*;
import flash.text.*;
public class Main extends Sprite
{
public function Main():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandeler);
}
private function keyDownHandeler(e:KeyboardEvent):void
{
if (e.keyCode == 27)
{
trace("Hello");
e.preventDefault();
}
}
}
}
I created a new project (FlashDevelop) targeting AIR 14 (also successfully tried AIR 3.9) with the following document class file:
package
{
import flash.desktop.NativeApplication;
import flash.display.NativeWindow;
import flash.display.NativeWindowInitOptions;
import flash.display.NativeWindowRenderMode;
import flash.display.NativeWindowSystemChrome;
import flash.display.Sprite;
import flash.display.StageDisplayState;
import flash.events.Event;
import flash.events.FullScreenEvent;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
if(e) removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyUpHandler);
var content:Sprite = new Sprite();
content.graphics.beginFill(0xFFFF00);
content.graphics.drawRect(0, 0, 400, 500);
content.graphics.endFill();
addChild(content);
this.addEventListener(MouseEvent.CLICK, fullScreen);
stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
}
private function fullScreen(e:Event):void {
stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
}
protected function keyUpHandler(event:KeyboardEvent):void {
switch(event.keyCode) {
case Keyboard.ESCAPE:
trace("ESCAPE");
event.preventDefault();
break;
default:
trace("UNHANDLED KEY: ", event.keyCode);
}
}
}
}
It worked as expected. When hitting escape, the preventDefault() method on the key event successfully kept the application in full-screen.
Notes:
It has to be on the key down. Key up had no effect. The result was the same with the key down listener on the stage or the nativeApplication.

AS3- ReferenceError: Error #1069: Property not found

Can someone tell me why I get this error?
ReferenceError: Error #1069: Property roll_mc not found on com.usmanzubairi.theAges.TheAges and there is no default value.
at com.usmanzubairi.theAges::PirGame/rolling()
package com.usmanzubairi.theAges
{
import flash.display.MovieClip;
import flash.events.*;
import flash.media.*;
public class TheAges extends MovieClip
{
private var game:PirGame;
private var game2:PreGame;
private var game3:SupGame;
public function TheAges()
{
stage.addEventListener(MouseEvent.CLICK, startGame);
}
private function startGame(event:Event):void
{
if (event.target != player_btn)
{
removeEventListener(MouseEvent.CLICK, startGame);
game = new PirGame();
addChild(game);
}
else
{
addChild(player_mc);
player_mc.visible = true;
player_mc.play();
}
if (event.target == player_mc.tom_mc)
{
removeEventListener(MouseEvent.CLICK, startGame);
game2 = new PreGame();
addChild(game2);
}
if (event.target == player_mc.pete_mc)
{
removeEventListener(MouseEvent.CLICK, startGame);
game = new PirGame();
addChild(game);
}
if(event.target == player_mc.sam_mc)
{
removeEventListener(MouseEvent.CLICK, startGame);
game3 = new SupGame();
addChild(game3);
}
}
public function gameOver():void
{
removeChild(game);
game = null;
stage.addEventListener(MouseEvent.CLICK, startGame);
}
}
}
Here's the PirGame document class code:
package com.usmanzubairi.theAges
{
import flash.utils.Timer;
import flash.events.*;
import flash.display.*;
import flash.geom.Matrix;
import flash.net.SharedObject;
public class PirGame extends MovieClip
{
public function PirGame()
{
addEventListener(MouseEvent.CLICK,rolling);
}
private function rolling (event:Event):void
{
if (event.target == MovieClip(root).roll_mc)
{
addChild(roll)
roll.visible = true;
runner_mc.visible = false;
roll.play();
}
}
}
}
Thanks.
If your roll_mc is in your PirGame symbol, and i think Pirgame class is well associated with the symbol, try modify your PirGame class a s follow:
package com.usmanzubairi.theAges
{
import flash.utils.Timer;
import flash.events.*;
import flash.display.*;
import flash.geom.Matrix;
import flash.net.SharedObject;
public class PirGame extends MovieClip
{
public function PirGame()
{
addEventListener(MouseEvent.CLICK,rolling);
}
private function rolling (event:Event):void
{
trace( event.target, roll_mc );
// change MovieClip(root).roll_mc to this.roll_mc as roll_mc is on this symbol and not on the root.
if( event.target == this.roll_mc )
{
trace( 'roll_mc clicked' );
addChild( roll )
roll.visible = true;
runner_mc.visible = false;
roll.play();
}
}
}
}
if you just want the roll_mc be clickable try the following:
package com.usmanzubairi.theAges
{
import flash.utils.Timer;
import flash.events.*;
import flash.display.*;
import flash.geom.Matrix;
import flash.net.SharedObject;
public class PirGame extends MovieClip
{
public function PirGame()
{
if( roll_mc ) roll_mc.addEventListener( MouseEvent.CLICK, rolling );
else addEventListener( Event.ADDED_TO_STAGE, _onStage );
}
private function _onStage( e:Event ):void
{
removeEventListener( Event.ADDED_TO_STAGE, _onStage );
roll_mc.addEventListener( MouseEvent.CLICK, rolling );
}
private function rolling( e:Event ):void
{
trace( 'roll_mc clicked' );
addChild( roll )
roll.visible = true;
runner_mc.visible = false;
roll.play();
}
}
}
if you don't have to re display the runner_mc you can remove it from the display list instead of make it not visible:
removeChild( runner_mc );

Inherit MovieClip From Another Class

Hey I seem to be having some problems inheriting Movie-clip from a class, I'm fairly new to as3, I've had a look around and can't tell if I'm doing something fundamentally wrong or not.
Let me show you
So I have a class I want to use to move EVERYTHING but the player sprite. So I want everything but the player to extend it. (or so I'm assuming.)
So I declare my class
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
public class everythingContainer extends MovieClip {
function brackets and so on...
(I'm just importing everything in an attempt to avoid errors)
I then have a class I want to inherit everythingContainer and Movieclip
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
import code.everythingContainer;
public class Tree1 extends everythingContainer {
Yet when I run this I get the error:
Line 1 5000: The class 'code.Tree1' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
Why am I getting this error?
Any help would be greatly appreciated!
I haven't got the full code to run yet so there may still be other obvious bugs laying about.
everythingContainer
Full code:
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
public class everythingContainer extends MovieClip {
var speed: Number = 4;
var wpressed: Boolean = false;
var apressed: Boolean = false;
var spressed: Boolean = false;
var dpressed: Boolean = false;
var xprev:int = 0;
var yprev:int = 0;
public function everythingContainer() {
// constructor code
trace ('Container started');
if(stage) init();
else
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
function init(eventInfo:Event = null):void
{
if(eventInfo != null)
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
trace ('Container init removed');
}
// constructor code
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
this.addEventListener( Event.ENTER_FRAME, containerEveryFrame);
}
public function containerEveryFrame (Event): void {
if (stage.contains(Main.player)) {
xprev = this.x;
yprev = this.y;
checkPlayerMovement();
}
}
// check the set keypress variables
public function checkPlayerMovement () : void {
if (wpressed) {
this.y -= this.speed;
}
if (spressed) {
this.y += this.speed;
}
if (apressed){
this.x -= this.speed;
}
if (dpressed) {
this.x += this.speed;
}
}
//assign key presses to variables
public function onKeyPress (event:KeyboardEvent):void {
//up
if (event.keyCode == 87){
wpressed = true;
}
//down
if (event.keyCode == 83) {
spressed = true;
}
//left
if (event.keyCode == 65){
apressed = true;
}
//right
if (event.keyCode == 68) {
dpressed = true;
}
}
//reset key press variables
public function onKeyRelease (event:KeyboardEvent) : void {
//up
if (event.keyCode == 87){
wpressed = false;
}
//down
if (event.keyCode == 83) {
spressed = false;
}
//left
if (event.keyCode == 65){
apressed = false;
}
//right
if (event.keyCode == 68) {
dpressed = false;
}
}
}
}
Main (there's some other stuff going on in here, but at the minute I'm just trying to get the trees working with my other class.)
package code
{
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import flash.geom.Rectangle;
import flashx.textLayout.container.ContainerController;
public class Main extends MovieClip
{
//public static var main
public static var player:PC;
//public static var firstenemy: WolfEnemy;
public static var MainContainer:everythingContainer;
public function Main()
{
// constructor code
trace('main started');
if (stage)
{
init();
}
else
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
}
function init(eventInfo:Event = null):void
{
if (eventInfo != null)
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
trace('Main init removed');
}
MainContainer = new everythingContainer ;
MainContainer.x = stage.stageWidth / 2;
MainContainer.y = stage.stageHeight / 2;
stage.addChild(MainContainer);
player = new PC();
player.x = stage.stageWidth / 2;
player.y = stage.stageHeight / 2;
stage.addChild(player);
//firstenemy = new WolfEnemy();
//firstenemy.x = 100;
//firstenemy.y = 100;
//stage.addChild(firstenemy);
stage.addEventListener( Event.ENTER_FRAME, everyFrameMain);
}
// check if an enemy hits the player.
/*public function enemycollison(): void {
if(firstenemy.hitTestObject(player)){
trace ('hit enemy');
player.health--;
firstenemy.kill();
}
}*/
// manage events that need to haapen globally for every frame
public function everyFrameMain(Event):void
{
/*if (stage.contains(firstenemy)){
enemycollison();
} */
//root.scrollRect = new Rectangle(player.x - 400, player.y - 300, 800, 600);
}
// finish and close game
public function endgame():void
{
}
}
}
and finally my tree class
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
import code.everythingContainer;
public class Tree1 extends everythingContainer {
public function Tree1()
{
// constructor code
trace ('Tree1 started');
if(stage) init();
else
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
function init(eventInfo:Event = null):void
{
if(eventInfo != null)
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
trace ('Tree1 init removed');
}
this.addEventListener( Event.ENTER_FRAME, tree1EveryFrame);
}
public function tree1EveryFrame (Event): void {
playercollision();
}
public function playercollision(): void {
if(this.hitTestObject(Main.player)){
trace ('hit wall');
Main.player.x = Main.player.xprev;
Main.player.y = Main.player.yprev;
}
}
}
}
Probably due to linkage errors inside Fla-file.
RMB on library-item: Tree1
Export for ActionScript + Export in frame 1 + "class: code.Tree1"
File/Publish Settings/Actionscript Settings (the small wrench, right of Script-dropdown)
Source Path (add the linkage to where the compiler can find your package-folder), usually for me I crate a folder next to the fla file called src or something like that so the code-file would be found at "/MyProject/src/code/Tree1.as", in that case I add "./src/" inside Source path inside Advanced ActionScript 3.0 settings
Added an example project in Flash CS6 found at url:
https://user.iter.org/filesharing/?uid=927205f7-cdfe-4915-a175-bc87f64af444
that is available for ~40 days.
Project structure in that file:
"/MyProject/DeepInheritage.fla"
"/MyProject/src/code/Foobar.as"
"/MyProject/src/code/Tree1.as"
Foobar.as which extends MovieClip
Tree1 library item which extends Foobar
That should be the exact same thing that you described in your issue, meaning that there is nothing wrong with that approach, it is just a matter of finding what is wrong. Most likely that is due to errors inside FLA-file, but might be something else.
code files:
package code {
import flash.display.MovieClip;
public class Foobar extends MovieClip {
public function Foobar() {
trace("foobar ctor()");
}
}
}
package code {
import flash.display.MovieClip;
public class Tree1 extends Foobar {
public function Tree1() {
trace("Tree1 ctor()");
}
}
}

Clicking button using keyboard in AS3

I am trying to do this by clicking on button using 'A' key in keyboard. I created two frames for this button but the code doesn't work, although there is no error.
Do I need to put anything in my main class? Can anyone help to fix this?
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class controlButton extends MovieClip {
public function controlButton() {
// constructor code
this.addEventListener(KeyboardEvent.KEY_DOWN,clickDown);
this.addEventListener(KeyboardEvent.KEY_UP,clickUp);
}
public function clickDown(event:KeyboardEvent):void{
// if the key is A
if(event.charCode == 65){
this.gotoAndStop(2);
}
}
public function clickUp(event:KeyboardEvent):void{
// if the key is A
if(event.charCode == 65){
this.gotoAndStop(1);
}
}
public function changelabel(newLabel:String):void{
this.label.text = newLabel;
}
}
}
Your button will never receive any KeyboardEvent. You should add your event listeners directly to the stage. Of course, you have to obtain a link to the stage. Anyways:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class controlButton extends MovieClip {
public function controlButton() {
// constructor code
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage (e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
//stage is no longer null here
stage.addEventListener(KeyboardEvent.KEY_DOWN,clickDown);
stage.addEventListener(KeyboardEvent.KEY_UP,clickUp);
}
public function clickDown(event:KeyboardEvent):void{
// if the key is A
if(event.charCode == 65){
this.gotoAndStop(2);
}
}
public function clickUp(event:KeyboardEvent):void{
// if the key is A
if(event.charCode == 65){
this.gotoAndStop(1);
}
}
public function changelabel(newLabel:String):void{
this.label.text = newLabel;
}
}
}
As you can see, you should add KeyboardEvent listeners to the stage right after the Event.ADDED_TO_STAGE fires.

Access of undefined property Keyboard (AS3)

I'm new to Actionscript 3 and I'm wanting to allow a circle to move down using the down arrow on the keyboard. Here's my code:
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Circle extends MovieClip {
public function Circle() {
// constructor code
var speed:int = 3;
addEventListener(KeyboardEvent.KEY_DOWN,keyIsDown);
function keyIsDown(event:KeyboardEvent) {
if(event.keyCode == Keyboard.DOWN) {
y = y+=speed;
}
}
}
}
}
When I test it, nothing happens when I press the down key. Anyone know what's wrong with the code?
Try adding KeyBoard events to the stage instead of to the class. Additionally, I would not nest functions like that, bad practice in general. Also the line y = y+=speed; is confusing, shouldn't it just be y += speed; ?
EDIT: Sorry, I guess stage will be null in the constructor, I've added a ADDED event listener.
Try this:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Circle extends MovieClip {
public function Circle() {
// constructor code
var speed:int = 3;
addEventListener(Event.ADDED, onAdded);
}
private function onAdded(event:Event) {
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyIsDown);
}
private function keyIsDown(event:KeyboardEvent) {
if(event.keyCode == Keyboard.DOWN) {
y += speed;
}
}
}
}