AS3 - Adding an object to a MovieClip Container from a class - actionscript-3

I'm kind of new to adding using classes in AS3, so far i've just been doing everything on frame1 of the movie. I figured I should eventually learn classes so here goes :) When I add objects onto the screen, I like to group them together in container objects for use later. So I have a board of Hex's that I'm building, and I'm trying to get them into a MovieClip named hexContainer that I have places on the stage. Now if I was doing this code like I normally would, I would just do hexContainer.addChild(tempHex). However, this is throwing me an error 1120.
My class code is as follows:
package
{
import flash.display.MovieClip
import Hex
import flash.display.Stage
public class Boards extends Hex
{
public function buildBoardOne()
{
for(var i:int = 1; i <= 5; i++)
{
var tempHex:Hex = new Hex();
tempHex.x = 100;
tempHex.y = 100;
hexContainer.addChild(tempHex);
}
}
}
}
I did in the beginning just have these added to the Stage and was getting an error when I did that, that's why the import statement is there. I had checked on google to figure out why and that's what they said to do.
Now, when I added these to the stage it worked fine. I could get my hex's and manipulate them and we partied and it was a great time. Now that I'm trying to put them into the container movie clip they are rather angry at me and I just can't appease them :p
Any help you guys could give me would be greatly appreciated.
Edited code to test out what okayGraphics suggested:
package
{
import flash.display.MovieClip
import Hex
import flash.display.Stage
public class Boards extends Hex
{
var hexContainer:MovieClip = new MovieClip();
stage.addChild(hexContainer);
public function buildBoardOne()
{
for(var i:int = 1; i <= 5; i++)
{
var tempHex:Hex = new Hex();
tempHex.x = 100;
tempHex.y = 100;
stage.addChild(tempHex);
}
}
}
}

You are getting an 1120 error because hexContainer is not defined in this package.
You either (1)need to declare var hexContainer = [your_reference_here] before you try to add children to it,
or
(2)you can just add the hexes as children to the Boards class, then add that to your hexContainer.
instead of hexContainer.addChild(TempHex); just put addChild(TempHex);
There's lots of other ways to do it too, but I think these two are the most straightforward approaches.

Related

Resolve Warning: 1082: Migration issue: Method gameThread will behave differently in ActionScript 3.0

Since I moved to using adobe flash 2017+, I've gotten warnings 1082/1083.
The warning is:
Warning: 1082: Migration issue: Method gameThread will behave differently in ActionScript 3.0 due to the change in scoping for the this keyword. See the entry for warning 1083 for additional information.
Although the only error which I am getting is for putting events inside classes, I cannot add mouse events or any events without having these warnings and I don't know how to fix...
People suggested to remove super() which is not the solution.
Example of event which I added to main code
error comes on line " addEventListener...."
package {
import flash.display.MovieClip;
import flash.events.Event;
import data.Player;
public class Game extends MovieClip {
private var players:Array = new Array();
public function Game() {
super();
resetPlayers();
addEventListener(Event.ENTER_FRAME, this.gameThread);
}
private function resetPlayers():void{
for (var i:int = 0; i < this.numChildren; i++){
if (getChildAt(i) is Player){
players.push(getChildAt(i));
}
}
}
protected function gameThread(event:Event):void{
for (var i:int = 0; i < this.players.length; i++){
players[i].fall();
}
}
}
}
You are be able to overcome this issue by simply removing the this keyword. It's not needed in this case.
addEventListener(Event.ENTER_FRAME, gameThread);
In AS3 methods are exectued in the context (your Game class) it's defined.
Same for the other uses of the this keyword.
players is a class variable of Game, resetPlayers() and gameThread() are methods of Game -> so it's all in the same context.

How to make a Grid, draw a shape in any of those cells and connect them together?

I have a very unique question regarding to my project
I have made a Grid including 25x15 cells using 3 classes:
First class named Tile.as and make cells for me:
package
{
import flash.display.Shape;
import flash.display.Sprite;
public class Tile extends Sprite
{
static const TILE_SIZE:int = 30;
public function Tile()
{
var tile:Shape=new Shape();
tile.graphics.lineStyle(3, 0x000000);
tile.graphics.drawRect(TILE_SIZE / 2, TILE_SIZE / 2, TILE_SIZE, TILE_SIZE);
this.addChild(tile);
}
}
}
The next class named Grid.as to integrate all Tile together:
package
{
import flash.display.Sprite;
public class Grid extends Sprite
{
static const GRID_SIZE:int = 25;
private var tileList:Vector.<Tile>;
public function Grid()
{
tileList = new Vector.<Tile>;
for (var v:int = 0; v < GRID_SIZE; v++)
{
for (var z:int = 0; z < GRID_SIZE-15; z++)
{
var tile:Tile = new Tile();
tile.x = v*Tile.TILE_SIZE;
tile.y = z*Tile.TILE_SIZE;
tileList.push(tile);
this.addChild(tile);
tile = null;
}
}
}
}
}
And then I showed Grid in my Main.as class and show it in the out put:
public class Main extends Sprite
{
//grid
private var grid:Grid;
//
public function Main()
{
//show the grid
grid = new Grid();
grid.x = 10;
grid.y = 10;
this.addChild(grid);
//
}
}
This is what I can see now:
My question is that i want to add two objects in this grid. For example:
object A will be located in one cell (Tile)
object B will be located in another cell (Tile)
and then I want to connect these 2 objects together by highlighting the dimensions of the cells!
this is an example of what I need to have:
Please ask me, if there is more explanation need to added!
And if I am going through the wrong way, please make me correct!
Thanks in advance!
If its about Path finding
i'm not sure if this question was about path finding!
but according #NealDavis comment:
i post a library for this possibility. however its a grid based path finding and also drawing, even with a fully clear grid.
from dauntless :
According to WikiPedia, A* (pronounced A Star) is
a best-first, graph search algorithm that finds the least-cost path
from a given initial node to one goal node (out of one or more
possible goals).
Here is a Live Demo
References to this library
documentation
source code

Remove Child created by class as3

I've been learning AS3 for a class project for just over a few months, most of what I know is by looking at how others problems were solved on here.
To the point.
I've got a class called Generate so I import that to my main timeline, in that class it creates a child and adds it.
Now I can't figure out how to remove it, it always says it's null:
"TypeError: Error #2007: Parameter child must be non-null."
package {
import MonsterOne;
import MonsterTwo;
import MonsterThree;
import MonsterFour;
import flash.display.*;
public class Generate extends MovieClip{
public static var monsterID = String(monsterID);
monsterID = Math.ceil(Math.random() * 4).toString();
public function Generate(parent:Object){
if (monsterID == 1){
var monsOne:MonsterOne = new MonsterOne();
monsOne.name = "monsterOne";
parent.addChild(monsOne);
monsOne.x = 100;
monsOne.y = 200;
trace("spawn1");
}
This is how it's generated. There is a little more code for monsTwo, three and four. Same code different names. That's all that is in the file.
I've tried all sorts to remove the code but always with an error and no actual removal of the child.
stage.removeChild(monsOne);
monsOne.parent.removeChild(monsOne);
removeChild(monsOne);
And who knows how many others.
Am I missing something or just doing it completely wrong.
Thank you
--EDIT--
if(o.monHP <= 0) {
turnTimer.stop();
turnTimer.removeEventListener(TimerEvent.TIMER, CounterA);
var monsOne:MovieClip = getChildByName('monsterOne') as MovieClip;
var monsTwo:MovieClip = getChildByName('monsterTwo') as MovieClip;
var monsThree:MovieClip = getChildByName('monsterThree') as MovieClip;
var monsFour:MovieClip = getChildByName('monsterFour') as MovieClip;
parent.removeChild(monsOne);
parent.removeChild(monsTwo);
parent.removeChild(monsThree);
parent.removeChild(monsFour);
gotoAndStop('win');
}
How I'm trying to remove the child.
package {
import MonsterOne;
import MonsterTwo;
import MonsterThree;
import MonsterFour;
import flash.display.*;
public class Generate extends MovieClip{
public static var monsterID = String(monsterID);
monsterID = Math.ceil(Math.random() * 4).toString();
public function Generate(parent:Object){
if (monsterID == 1){
var monsOne:MonsterOne = new MonsterOne();
monsOne.name = "monsterOne";
parent.addChild(monsOne);
monsOne.x = 100;
monsOne.y = 200;
trace("spawn1");
}
if (monsterID == 2){
var monsTwo:MonsterTwo = new MonsterTwo();
monsTwo.name = "monsterTwo";
parent.addChild(monsTwo);
monsTwo.x = 100;
monsTwo.y = 200;
trace("spawn2");
}
if (monsterID == 3){
var monsThree:MonsterThree = new MonsterThree();
monsThree.name = "monsterThree";
parent.addChild(monsThree);
monsThree.x = 100;
monsThree.y = 200;
trace("spawn3");
}
if (monsterID == 4){
var monsFour:MonsterFour = new MonsterFour();
monsFour.name = "monsterFour";
parent.addChild(monsFour);
monsFour.x = 100;
monsFour.y = 200;
trace("spawn4");
}
}
}
}
Full Generate File
There's so many things wrong with your code that it's not easy to know where to begin. You lack very basic understanding of the display list and most important scope.
Display List:
when you do something like this:
var monsOne:MovieClip = getChildByName('monsterOne') as MovieClip;
parent.removeChild(monsOne);
That can be translated to "no idea what I'm doing here". Why? Because the code means it expects a DisplayObject named "monsterOne" to exist in the display list of "this" (the display list in scope) and then tries to remove that object from "parent". None of this makes sense, if the object exist in display list A then remove it from display list A not from display list B.
Programming:
You are using variable and parameters that mirror existing properties, ex: "parent". Are you sure at your level of programming you can afford to confuse yourself with that kind of dangerous programming behavior? The answer is no, don't mirror properties names.
Scope:
You keep losing scope all over the place but keep writing code like scope doesn't even exist. Stop that. A variable named monsOne in one scope doesn't exist in any other scope, period.
More weirdness:
public static var monsterID = String(monsterID);
monsterID = Math.ceil(Math.random() * 4).toString();
You do not know what this does obviously or else you would not do it that way.
What does your code do?:
It creates one and one only movieclip type and each time it creates it it gives it the exact same name and add it to a phantom "parent" we know nothing about. Then when you try to remove it you don't even know or remember what this phantom parent is and you can't really use getChildByName() either cos you need to know the parent in order to do that and even worse by then so many MovieClip might be there and all have the same name.
How to fix?
This section will be filled later as you ask specific questions on how to fix what.
The Generate class generates only one monster. In the gameOver function you are trying to remove all 4 monsters and you get an error that the monster doesn't exist. You should add condition:
if (monsOne) removeChild(monsOne);
if (monsTwo) removeChild(monsTwo);
if (monsThree) removeChild(monsThree);
if (monsFour) removeChild(monsFour);

AS3 How to use addChild() in a separated as file

How can I add the MovieClip I have put into the array to the Stage?
The following code is a separated .as file and located at the same level with the main.fla
I have tried many times but I got the error message -
"ReferenceError: Error #1065: Variable stage is not defined. at Set1()
at main_fla::MainTimeline/frame1()"
How can I do? Thank for any help!!
package
{
import flash.display.MovieClip;
import flash.display.Stage;
public class Set1
{
private var map:Array=new Array();
public function Set1()
{
for (var i:Number=0; i<5; i++)
{
var cell_mc=new cell();
cell_mc.x = 50+ i*cell_mc.width;
cell_mc.y = 50;
cell_mc.className=i;
map[i] = cell_mc;
trace(map[i].className);
stage.addChild(map[i]);
}
}
}
}
You are a little mixed up. stage is not a magic variable, instead it is a property that is inherited from the DisplayObject base class. That property gets set internally when a display object is added to the stage. So in your case your class needs to either inherit from a DisplayObject– probably Sprite class. Or simply inject a reference to the Stage from the outside when you invoke your function
First you need to set the Main flash file class.You'll do that by clicking on stage in your fla. file and edit you class in properties(should look like this(class:Set1))
Code below should work fine
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
public class Set1 extends Sprite
{
private var map:Array=new Array();
public function Set1()
{
for (var i:Number=0; i<5; i++)
{
var cell_mc=new cell();
cell_mc.x = 50+ i*cell_mc.width;
cell_mc.y = 50;
cell_mc.className=i;
map[i] = cell_mc;
trace(map[i].className);
addChild(map[i]);
}
}
}
}

Actionsript 3.0 control multiple Instances of a class using a single function

Hey I'm new to Flash AS 3.0 and I am having trouble with creating instances of classes and want to Control them using a single function for all of them.
import flash.display.MovieClip;
import flash.events.*;
stage.addEventListener (KeyboardEvent.KEY_DOWN, movestuff);
var newsymbol:MovieClip;
newsymbol = new Symbol1;
addChild(newsymbol);
newsymbol.x = 200
newsymbol.y = 200
addChild(newsymbol);
function movestuff (event:KeyboardEvent):void
{
newsymbol.x + 100
}
Symbol1 is a class from the library that I am trying to move, and I want to add multiple instances of it but control all of them using the function movestuff
Keep the instances in an array:
var penguinArray:Array = [];
function addPenguin(){
var newPenguin:Penguin = new Penguin();
//do stuff
penguinArray.push(newPenguin);
}
function moveStuff(){
for(var i in penguinArray){
penguinArray[i].x += 10;
}
}
Is your movement that uniform between all of them (do you want to move them all by the same x and y)? If so, put them all inside one parent movie clip and only move that.