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);
}
Related
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.
All late comers this question in still active a answer is not yet reached, what you might see below is a irrelevent syntax error a nice member found for me
error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Player()
at Maintest_fla::MainTimeline/createPlayer()
When i'm attempted to add the instance name wall0x objects that are in the object with the instance name world, I find that I get a null object error.
Also ignore the long list of variables, not relevant.
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.filters.BlurFilter;
import flash.utils.Timer;
public class Player extends MovieClip
{
// player settings
private var _rotateSpeedMax:Number = 20;
public var _gravity:Number = .10;
// projectile gun settings
public var _bulletSpeed:Number = 4;
public var _maxDistance:Number = 200;
public var _reloadSpeed:Number = 250;//milliseconds
public var _barrelLength:Number = 20;
public var _bulletSpread:Number = 5;
// gun stuff
private var _isLoaded:Boolean = true;
private var _isFiring:Boolean = false;
private var _endX:Number;
private var _endY:Number;
private var _startX:Number;
private var _startY:Number;
private var _reloadTimer:Timer;
private var _bullets:Array = [];
// array that holds walls
public var _solidObjects:Array = [];
//
private var _player:MovieClip;
private var _dx:Number;
private var _dy:Number;
private var _pcos:Number;
private var _psin:Number;
public var _trueRotation:Number;
public function Player()
{
// constructor code //Right hereVVVthe instance name is wall0x and it's in the object world on the stage.
_solidObjects = [MovieClip(root).world.wall01,MovieClip(root).world.wall02,MovieClip(root).world.wall03,MovieClip(root).world.wall04];
/*addEventListener(Event.ENTER_FRAME, enterFrameHandler);
addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);*/
}
}
}
Code I'm using in frame 2 create the player and then continuously set it's chords to another objects.
stage.addEventListener(Event.ENTER_FRAME, createPlayer);
function createPlayer(e:Event):void
{
// attach player movieclip from library
// position player in center
if (character!=null&&_player!=null)
{
_player.x = character.x + 5;
_player.y = character.y + 5;
}
else if (_player ==null && world.wall01 != null)
{
var _player:Player;
_player = new Player();
// add to display list
stage.addChild(_player);
}
}
First: You have a syntax error in these two lines:
_player.x = MovieClip.(root).character.x + 5;
_player.y = MovieClip.(root).character.y + 5;
There should not be a period after MovieClip, so it should look like this:
_player.x = MovieClip(root).character.x + 5;
_player.y = MovieClip(root).character.y + 5;
Second: You are always creating a new Player every frame. In your createPlayer method, you have the following conditional:
if(character != null && _player != null) //_player is not a defined in this scope, so it will either throw an error, or always return null/undefined
You do not have a _player var defined in the scope of that frame or the scope of the createPlayer method, you have defined it inside the scope of the else statement (which makes only available in the else statement)
Move the var _player:Player to the top of your timeline code with the other frame scoped vars.
Third: you are trying to access root in your Player constructor, the problem with this, is that when the contructor runs, your Player is not yet on the display tree, so root is null until you've added player to the stage.
example:
_player = new Player(); //this will run your contructor, but root will be null
stage.addChild(_player); //after this, your Player class will now have access to root/stage/parent object
Change your Player class so it listens for being ADDED_TO_STAGE before trying access root.
public function Player()
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
// constructor code
}
private function init(e:Event):void {
this.removeEventListener(Event.ADDED_TO_STAGE, init);
_solidObjects = [MovieClip(root).world.wall01,MovieClip(root).world.wall02,MovieClip(root).world.wall03,MovieClip(root).world.wall04];
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
}
I'm developing a mobile project with Flash Builder, and I'm having some issues with the Tween Class... It works just fine for some time, but then, all of the sudden, it just stops working.
The game emulates an "under the sea" scene, so, one of the things I have is a "bubbly" background, and it works just fine for some time, and then, all of the sudden, all the tweens stop at the same time (including some tweens that are in other classes). Any thoughts on why this could happen? I'm adding the code of the background bubbles to see if you get why this could be behaving so oddly.
Thanks in advance!
public class BurbujasBack extends Sprite
{
public var timer:Timer = new Timer(200,0);
public var burbujasMaximas:int = 25;
public static var burbujasActuales:int = 0;
public function BurbujasBack()
{
x=Diversion_bajo_el_agua.ancho_st/2;
y=Diversion_bajo_el_agua.alto_st;
timer.addEventListener(TimerEvent.TIMER,_nuevaBubble);
timer.start();
}
private function _nuevaBubble(e:TimerEvent):void {
var add:Boolean = (Math.random() > .5) ? true : false;
if (add==true) {
if(burbujasActuales < burbujasMaximas) {
trace("agrega una burbuja");
var burbuja:BurbujaChica = new BurbujaChica();
addChild(burbuja);
burbujasActuales++;
}
}
}
}
And
public class BurbujaChica extends Sprite
{
public var velocidad:Number = Math.random();
public var escala:Number = Math.random()*.5+.5;
public var tiempoMaximo:int = 3;
public var posX:Number = Math.random()*Diversion_bajo_el_agua.ancho_st;
public function BurbujaChica()
{
x = posX
var burbuja:Burbuja_ = new Burbuja_();
burbuja.scaleX = burbuja.scaleY = escala;
var desde:Number = burbuja.height/2;
var hasta:Number = -Diversion_bajo_el_agua.alto_st-burbuja.height/2;
trace("va desde "+desde+" hasta "+hasta);
var tw:Tween = new Tween(burbuja,"y",Regular.easeIn,desde,hasta,(velocidad*tiempoMaximo)+5,true);
tw.addEventListener(TweenEvent.MOTION_FINISH,_chauBurbuja);
addChild(burbuja);
}
private function _chauBurbuja(e:TweenEvent):void {
this.parent.removeChild(this);
BurbujasBack.burbujasActuales--;
}
}
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);
I am making a website in Flash, coded in flashbuilder. Whenever I try to export my code I get the same error again and again (TypeError = see below).
I think the problem has something to do with the stage of my project. Whenever I change the var stageMiddenX = (stage.stageWidth / 2); into var stageMiddenX = 512;, the code works. but I wan't the var to be dynamic.
TypeError
Error #1009: cannot access a property or method of a null object reference at main()
package {
import flash.display.MovieClip;
public class main extends MovieClip{
var stageMiddenX = (stage.stageWidth / 2);
var stageMiddenY = (stage.stageHeight / 2);
private var object1:Object1 = new Object1();
private var object2:Object2 = new Object2();
private var object3:Object3 = new Object3();
}
}
The issue here is that stage is not yet available at the time you are requesting it.
You'll want to wait until the Event.ADDED_TO_STAGE event is fired before attempting to acccess stage.
package {
import flash.display.MovieClip;
public class main extends MovieClip{
private var object1:Object1 = new Object1();
private var object2:Object2 = new Object2();
private var object3:Object3 = new Object3();
private var stageMiddenX:Number;
private var stageMiddenY:Number;
public function main(){
if(stage) init(null);
else addEventListener(Event.ADDED_TO_STAGE, init)
}
private function init(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
stageMiddenX = (stage.stageWidth / 2);
stageMiddenY = (stage.stageHeight / 2);
}
}
}
Put the stuff accessing stage into a constructor (assuming this is your document class)..
package
{
import flash.display.MovieClip;
public class main extends MovieClip
{
public var stageMiddenX:int;
public var stageMiddenY:int;
private var object1:Object1 = new Object1();
private var object2:Object2 = new Object2();
private var object3:Object3 = new Object3();
public function main()
{
stageMiddenX = stage.stageWidth / 2;
stageMiddenY = stage.stageHeight / 2;
}
}
}