AS3 object array to class - actionscript-3

I am new and having an issue with the use of classes in as3.
I have created an array of objects in my main timeline
function badPlayer()
{
var bads:Array = new Array();
for (var i=0; i<5; i++)
{
var mc = new bman();
mc.name=["mc"+i];
bads.push(mc);
_backGround.addChild(mc);
mc.x = 100;
mc.y = 100;
trace (bads);
Baddies(_backGround.mc); //here I am trying to export mc to my class
}
}
Here is a snip-it from my class. My trace statement wont even output.
public class Baddies extends MovieClip
{
private var pistolSound:pistolShot = new pistolShot();
//private var mc = new mc();
private var _rotateSpeedMax:Number = 2;
private var _gravity:Number = .68;
private var _bulletSpeed:Number = 2;
private var _maxDistance:Number = 200;
private var _reloadSpeed:Number = 500; //milliseconds
private var _barrelLength:Number = 20;
private var _bulletSpread:Number = 5;
private var _isLoaded:Boolean = true;
private var _isFiring:Boolean = true;
private var _endX:Number;
private var _endY:Number;
private var _startX:Number;
private var _startY:Number;
private var _reloadTimer:Timer;
private var _bullets:Array = [];
private var _gun:MovieClip;
private var _enemy:MovieClip;
private var _yx:Number;
private var _yy:Number;
private var _pcos:Number;
private var _psin:Number;
private var _trueRotation:Number;
public function Baddies()
{
trace("working");
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
Basically I am trying to create several bad guys (bman) and have the same code apply to each of them. I have also tried to change the linkage name of bman to Baddies with no success.

There are a few thing that are very wrong with this code.
Baddies(_backGround.mc); //here I am trying to export mc to my class
This is a typecast, as already stated in the comments. By the way Baddies isn't a good name, because it plural. You probably want to create a new bad guy, which would be done with this line:
var baddie = new Baddies();
Now your constructor uses the stage variable. This won't work because the object isn't on the stage, therefore stage is null (it may works if you drag and drop an instance to the stage in the editor). So before using the stage you actually need to add the object to the stage:
public function Baddies() {
trace("new baddie created");
}
public function init(mc:MovieClip) {
mc.addChild(this); // display this baddie
trace("working");
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
And in the badPlayer function:
var baddie = new Baddies();
baddie.init(_backGround);

Related

Error 1119 in Actionscript 3 (as3) Access of possibly undefined property text through a reference with static type money_txt

This is in the main class
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.events.Event;
public class main extends MovieClip {
public var scene = 0;
public var _money = 0;
public var gain = 1;
public var clicks = 0;
public function main() {
addEventListener(Event.ENTER_FRAME, loop);
mainbtn.addEventListener(MouseEvent.CLICK, handler);
playbtn.addEventListener(MouseEvent.CLICK, playHandler);
}
var mainbtn:button = new button();
var playbtn:playbutton = new playbutton();
var playtxt:playtext = new playtext();
var cash:money_txt = new money_txt();
var scene0:MovieClip = new MovieClip();
var scene1:MovieClip = new MovieClip();
public function loop(e:Event):void {
if(scene == 0) {
addChild(scene0)
scene0.addChild(playbtn);
playbtn.x = 300;
playbtn.y = 200;
scene0.addChild(playtxt);
playtxt.x = 300;
playtxt.y = 100;
} else {
scene0.removeChild(playbtn);
scene0.removeChild(playtxt);
}
if(scene == 1) {
addChild(scene1);
scene1.addChild(mainbtn);
mainbtn.x = 300;
mainbtn.y = 200;
scene1.addChild(cash);
cash.text = 'Money: ' + _money.toString();
} else {
scene1.removeChild(mainbtn);
}
}
public function playclickHandler(e:MovieClip) {
scene = 1;
}
public function handler(e:MouseEvent):void {
_money += gain;
clicks++;
trace('yep');
}
public function playHandler(e:MouseEvent):void {
scene = 1;
}
}
}
And This is where the error would be
C:\Users\Slime\Desktop\Art-ish\game\main.as, Line 47, Column 10 1119: Access of possibly undefined property text through a reference with static type money_txt.
Thanks for helping if you can!
these should be defined as public
public var mainbtn:button = new button();
public var playbtn:playbutton = new playbutton();
public var playtxt:playtext = new playtext();
public var cash:money_txt = new money_txt();
public var scene0:MovieClip = new MovieClip();
public var scene1:MovieClip = new MovieClip();
also it is hard to tell if money_txt, playtext, playbutton and button are classes or MovieClip instances. Convention dictates that Classes should start with a capital letter and instances with lower.
update
The issue is that if button and playbutton are buttons and playtext and money_txt are MovieClips, you should instantiate them as such.
for example if you have
public var mainbtn:button = new button();
but there is no class with name of button, mainbtn will be null. What you may need to do is
public var mainbtn:Button;
public var cash:MovieClip;
and as a part of your main or some other function, assign the instances
mainbtn = this['button'];
cash = this['money_txt'];
you can check if this worked by checking trace(cash);, which will return null if the assignment did not work.
I should stress again though, it is hard to to know what exactly is going wrong without knowing what your setup is. I'm assuming money_txt and the other classes you are defining are not actually classes with their own linkage IDs, but buttons and movieclips inside the MovieClip or stage you are putting this code in.

Pre-render as3 animation

I need to pre render animation, which I am creating by code in as3. I would like to save every frame of _debugBmp to *.png or *.bmp file, or create sprite sheet.
Is that possible?
Thank you for answer.
public class PerlinNoise extends Sprite
{
// premenne pre perlin noise
private var _baseX:Number = 45;
private var _baseY:Number = 5;
private var _numOctaves:uint = 3;
private var _randomSeed:int = 50;
private var _stitch:Boolean = true;
private var _fractalNoise:Boolean = false;
private var _channelOptions:uint = 1;
private var _grayScale:Boolean = true;
private var _offsets:Array = [];
private var _perlinBitmapData : BitmapData;
private var _debugBmp : Bitmap;
public function PerlinNoise()
{
_perlinBitmapData = new BitmapData(275, 50, true);
// oktavy perlin noisu
for(var i:int = 0; i < _numOctaves;i++) _offsets[i] = new Point(0,0);
_debugBmp = new Bitmap(_perlinBitmapData);
addChild(_debugBmp);
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void
{
// animacia perlin noisu
_offsets[1]['x'] += 1; // 2
_offsets[1]['y'] += 1/4;//1/4
// aplikacia perlin noisu
_perlinBitmapData.perlinNoise(_baseX, _baseY, _numOctaves, _randomSeed, _stitch, _fractalNoise, _channelOptions, _grayScale, _offsets);
}
}
I would recommend putting your code in an Adobe AIR application and then save the BitMapData out to files after each image is created in the onEnterFrame method. Once you have al your images than you could make a spritesheet out of them.

1009 error on a movieclip that makes no sense

I'm making a game, and originally I placed the hero movieclip on the stage manually. Now I add the hero to a container and load the container in the Main.as constructor.
I get a 1009 error for this line:
bulletOffset = 5 / _root.accuracy;
Here is the relevant code of the hero class:
public class Hero extends MovieClip {
private var radius:Number;
//Bullet offset
private var bulletOffset:Number;
//Player variables
private var walkingSpeed:int = 3;
private var shootingRate:int = 120;
private var s:int;
//Making all of the items on the stage accessible by typing "_root.[ITEM]"
private var _root:MovieClip;
private var leftKeyDown:Boolean = false;
private var upKeyDown:Boolean = false;
private var rightKeyDown:Boolean = false;
private var downKeyDown:Boolean = false;
private var punchKeyDown:Boolean = false;
//Player states (shooting, attacking etc)
private var shooting:Boolean = false;
public function Hero()
{
addEventListener(Event.ADDED, beginClass);
}
private function beginClass(event:Event):void
{
//Determine the radius
radius = this.width - 8;
_root = MovieClip(root);
bulletOffset = 5 / _root.accuracy;
blablablabla
You are listening to the wrong event. Event.ADDED will be fired when it's added to any display list. But you need to wait for Event.ADDED_TO_STAGE before root will be available to you:
public function Hero()
{
addEventListener(Event.ADDED_TO_STAGE, beginClass);
}

Best way of passing values from a button with as3 event

I develop my own way of doing this simple task. However, I'm now wondering if there is a better way to do it.
The buttons:
var menuBtnEscrit:MovieClip = new MovieClip();
var mbe:btnEscritorio = new btnEscritorio();
menuBtnEscrit.addChild(mbe);
menuBtnEscrit.val = "escrit";
menuBtnEscrit.x = 80;
menuBtnEscrit.addEventListener(MouseEvent.CLICK, novoCont);
dMenu.addChild(menuBtnEscrit);
var menuBtnPublic:MovieClip = new MovieClip();
var mbp:btnPublic = new btnPublic();
menuBtnPublic.addChild(mbp);
menuBtnPublic.val = "public";
menuBtnPublic.x = 244;
menuBtnPublic.addEventListener(MouseEvent.CLICK, novoCont);
dMenu.addChild(menuBtnPublic);
And I can keep going, or create buttons trough some algorithm, and put more properties and take advantage of a MovieClip.
The handler:
private function novoCont(e=null){
if(e!=null) selecCont = new String(e.target.parent.val);
clearDisplay(dSubMenu);
clearDisplay(dConteudo);
var func:String = "cont_"+selecCont;
this[func]();
}
As you can see, there is some common task for all buttons.
If I click the first button, it will call cont_escrit() function. This mechanics works, but it is the best practice? Is there a way of optimize it?
public class BaseButton extends Button {
private var _func:Function;
public function get handler():Function {
return _func;
}
public function set handler(value:Function):void {
_func = value;
}
}
So hero you can create mbe like this
mbe = new BaseButton ();
mbe.hanler = cont_escrit;
The event hanler would be
private function novoCont(e=null){
if(e!=null) {
var selecContent:BaseButton = e.targe as BaseButton ;
var handler:Function = selecContent.handler;
handler();
}
}
Here is an example.
var menuBtnEscrit:MovieClip = new MovieClip();
var mbe:btnEscritorio = new btnEscritorio();
menuBtnEscrit.addChild(mbe);
//menuBtnEscrit.val = "escrit";
menuBtnEscrit.func = this.cont_escrit;
menuBtnEscrit.x = 80;
menuBtnEscrit.addEventListener(MouseEvent.CLICK, novoCont);
private function novoCont(e=null){
if(e!=null) {
var mc:MovieClip = e.target as MovieClip;
var func:Function = mc.func;
func();
}
clearDisplay(dSubMenu);
clearDisplay(dConteudo);
}

Invalid BitMapdata Error #2015

private static var tileDir:String;
public static var tileImage:BitmapData;
private static var loader:Loader;
public static var tileReady:EventDispatcher = new EventDispatcher();
public static var tileNum_perRow:Number = 3;
public static var select:selector;
public var tileNum:Number = 0;
public var setBack:Number = 0;
public var tileState:String;
public var tCol:Number;
public var tRow:Number;
private var tWidth:int = 30;
private var tHeight:int = 30;
private var tImage:BitmapData;
private var canvas:Bitmap;
private var p:Point;
private var r:Rectangle;
public function Tile():void
{
var tempImage:BitmapData = tileImage;
r = new Rectangle(0,0,30,30);
p = new Point(0,0);
tImage = new BitmapData(30,30);
tImage.copyPixels(tempImage,r,p);
}
And I get the following error:
ArgumentError: Error #2015: Invalid BitmapData.
It tends to happen when I copy the pixels. I dont know why.
tileImage;
tileImage is valid, I've used the variable in the code below and it works. But above doesnt. Don't know why. Anyone have any clues?
this.graphics.beginBitmapFill(tileImage);
this.graphics.drawRect(60, 0,tWidth ,tHeight );
Nevermind, I figure a better solution
theMatrix.translate(30,0);
this.graphics.beginBitmapFill(tileImage,theMatrix);
//this.graphics.drawRect(0, 0,tWidth ,tHeight );
this.graphics.endFill();
using the theMatrix.translates give you the ability to choose what part of the tile you want, without effecting the position of the sprite