as3 Error 1009 at Coin1/coin1go(), i am trying to get an enemy to drop a coin - actionscript-3

So the enemy does drop a coin but i does not get the properties of the coin( if it hits the player it gives him +5 coins)
The coin will be removed if it hits the bottom of the stage, if the player dies or if the player hits it. Sadly, it does not work.
But this does work if i place a coin on the stage before i start the game, it gets all its properties, so then it must be the moment it gets added to the stage that it does not get linked with the coding or something..... and that is where i am right now.
this is the .as file for the coin:
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class Coin1 extends MovieClip
{
private var _root:Object;
private var speed:int = 0;
public function Coin1()
{
addEventListener(Event.ENTER_FRAME, Speed1);
addEventListener(Event.ADDED, beginClass);
addEventListener(Event.ENTER_FRAME, coin1go);
}
private function beginClass(event:Event):void
{
_root = MovieClip(root);
}
private function Speed1(event:Event):void
{
y += speed;
}
private function coin1go(event:Event):void
{
if (this.y > stage.stageHeight)
{
removeEventListener(Event.ENTER_FRAME, coin1go);
_root.removeChild(this);
}
if (hitTestObject(_root.player_mc))
{
_root.coin += 1;
removeEventListener(Event.ENTER_FRAME, coin1go);
_root.removeChild(this);
}
if (_root.playerhealth <= 1)
{
removeEventListener(Event.ENTER_FRAME, coin1go);
_root.removeChild(this);
}
}
}
}
This is the part from where it gets added to the stage:
if (enemy2health <= 0)
{
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.score += _root.Enemy2Score * _root.scoremultiplyer;
stage.addChild(newExplosionSmall)
newExplosionSmall.x = this.x;
newExplosionSmall.y = this.y;
stage.addChild(newCoin1)
newCoin1.x = this.x;
newCoin1.y = this.y;
Ass you can see there is also an addchild for an explosion wich works perfectly fine but that may jus be because it does nothing else than appear and remove itself.
So long story short: enemy drops coin but it does nothing and floats to the bottom of the screen and i get a constant stream of 1009 errors. so does anyone know how to fix this?

You should add an enterframe listener only after receiving a valid stage reference, which only appears when Event.ADDED_TO_STAGE event is received. This is because your enterframe listener refers stage.
public function Coin1()
{
addEventListener(Event.ENTER_FRAME, Speed1);
addEventListener(Event.ADDED_TO_STAGE, beginClass);
}
private function beginClass(event:Event):void
{
_root = MovieClip(root);
addEventListener(Event.ENTER_FRAME, coin1go);
}

Related

As3: Bullet giving me errors?

I have an enemy class which spawns bullets at the player.
private function fireBullet()
{
if(isFiring)
{
fire();
}
}
public function fire():void
{
var bullet:Bullet = new Bullet(x, y, rotation);
stage.addChild(bullet);
}
And in the bullet class:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Bullet extends MovieClip {
private var _root:MovieClip;
private var isVanished:Boolean = false;
public function Bullet(x:int, y:int, rotation:Number)
{
this.x = x;
this.y = y;
this.rotation = rotation;
_root = MovieClip(root);
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop (event:Event):void
{
if(this.hitTestObject(_root.assassin.hitbox))
{
_root.hitPoints -= 30;
}
else
{
y-=Math.cos(rotation/-180*Math.PI)*(15);
x-=Math.sin(rotation/-180*Math.PI)*(15);
}
if(this.x < 0 || this.x > _root.stageWidth || this.y > _root.stageWidth || this.y < 0)
{
removeChild(this);
removeEventListener(Event.ENTER_FRAME, loop);
}
}
}
}
However, when I start the game, I get a 1009 error regarding line 23, and the game slows down rapidly as the bullets don't even move.
I also get an 1063 error, expecting 3 but having 0.
ArgumentError: Error #1063: Argumentblabla for Bullet(). Expected 3 but 0 were shown. ((translated))
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at Main()[C:\Venture Inc\Main.as:189]
This is what main looks like
//Constructor
public function Main()
{
addChild(container_staff);
addChild(container_wall);
etc etc etc
ArgumentError: Error #1063: Argumentblabla for Bullet(). Expected 3 but 0 were shown. ((translated))
Bullet class constructor has 3 arguments. Check your code more precisely, I think you have something like this:
var bullet: Bullet = new Bullet();
Also there is problem with your code. Every bullet subscribes for the ENTER_FRAME. Centralize your game, create main loop, and update all game actors(bullets, enemies, etc) from there.

Pong: How to pass position of ball on the stage to the cpu paddle? -AS3

I am a beginner with AS3 and I am trying to make a basic Player vs CPU pong game(using a tutorial as reference). I apologize if this sounds stupid or obvious. I have a document class and individual classes for Ball, Player and CPU. My problem is that I don't know how to make CPU class use the co-ordinates of the movie clip ball on the stage so that it can move with respect to the ball as required to form the AI. The tutorial I have been referring has all the code for ball, player and cpu in the document class only but I have written code for everything in their respective classes.
The tutorial link http://as3gametuts.com/2011/03/19/pong-1/
My version of code. Currently the ball is bouncing off the walls but HitTest has not been applied to anything. The player paddle is moving with the arrow keys. No warnings or errors are being displayed.
Main
public class Main extends MovieClip
{
public function Main()
{
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
trace("Initialized");
}
}
Ball
public class Ball extends MovieClip
{
public var ballSpeedX:int = 5;
public var ballSpeedY:int = 6;
public function Ball()
{
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage)
}
public function onAddedToStage(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage)
this.x = stage.stageWidth / 2;
this.y = stage.stageHeight / 2;
this.addEventListener(Event.ENTER_FRAME, loop)
}
public function loop(e:Event):void
{
this.y += ballSpeedY;
this.x += ballSpeedX;
if ((this.y <= 0 + this.height/2) || (this.y >= stage.stageHeight - this.height/2))
{
ballSpeedY *= -1;
}
else if ((this.x <= 0 + this.width/2) || (this.x >= stage.stageWidth - this.width/2))
{
ballSpeedX *= -1;
}
}
}
TheCpu
public class TheCpu extends MovieClip
{
public var cpu:TheCpu;
public var ball:Ball;
private var vx:Number = 5;
public function TheCpu()
{
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
this.x = 240;
this.y = 10;
this.addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void
{
/*if (this.x < ball.x - 10)
{
this.x += vx;
}
else if (this.x > ball.x + 10)
{
this.x -= vx;
}*/
}
}
I added this to my main class
private function onAddedToStage(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
trace("It's Alive!");
addBall();
addCPU();
}
public function addBall():void
{
var ball:MovieClip = stage.getChildByName("ball") as MovieClip
stage.addChild(ball);
}
public function addCPU():void
{
var cpu:MovieClip = stage.getChildByName("cpu") as MovieClip
stage.addChild(cpu);
}
}
but now it is giving error
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/addChild()
at flash.display::Stage/addChild()
at src::Main/addBall()
at src::Main/onAddedToStage()
and if I use
var ball:Ball = new Ball();
var cpu:TheCpu = new TheCpu();
I get
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at src::TheCpu()
at src::Main/addCPU()
at src::Main/onAddedToStage()
I think I am being really dumb now.
As a simpler solution.
Basically when you create the ball you could pass the instance of the ball to the CPU so that the CPU can just check where the ball is on each update/enter frame. Something like this:
var ball:Ball = new Ball();
var cpu:CPU = new CPU(ball);
I think ideally you would have an update loop that just tells each part to update itself in sequence and passes along any important information between the objects so they can update themselves appropriately (and the main update loop can track things like ball out of region/point scored etc.)
EDIT
If I understand you correctly you've placed symbols on the stage in Flash and have assigned these code blocks to each of them. So your issue is you don't know how to reference each of the instances. You could use this method to retrieve the instances you put on the stage by naming them and referring to them by name:
How do I access a movieClip on the stage using as3 class?
Alternatively you could create the instances in the Main class in the added to Stage handler by creating instances as shown in the code block above then calling:
stage.addChild(cpu);
stage.addChild(ball);
Just be sure to use the MovieClip symbol name from the library in place of CPU and Ball.
In your commented-out code in the TheCpu class, you put "ball.y" when I think you meant "ball.x".

Make Flash Parent Fade Out & adding sound BrickBreaker Game

Hi i am trying to get this brick to fade out when the ball hits it in my brickbreaker game in flash AS3. Here is the code. At the moment there is just a removechild function which makes it just dissapear i want to know how to make it fade out instead. Also i have a breaking sound i would like to add when the ball hits the brick and wonder how i would add this aswell?
EDIT: I have managed to add sound by using Var & Play after the remove child line
package {
import flash.display.*;
import flash.events.*;
public class Brick extends MovieClip {
private var _root:MovieClip;
public function Brick(){
addEventListener(Event.ADDED, beginClass);
addEventListener(Event.ENTER_FRAME, enterFrameEvents);
}
private function beginClass(event:Event):void{
_root = MovieClip(root);
}
private function enterFrameEvents(event:Event):void{
if(this.hitTestObject(_root.Ball)){
_root.ballYSpeed *= -1;
this.parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME, enterFrameEvents);
}
}
}
}
No need for any tweener pack for just one tween.
You can use the Tween class provided in AS3 itself. Try this :
new Tween(mc,"alpha",
Strong.easeIn,
mc.alpha,
0,
2,
true).addEventListener(
TweenEvent.MOTION_FINISH,
function() { removeChild(mc); },
false, 0, true);
Note:
mc is the movieclip (or the brick).
The code removes the movieclip from stage after the tween completes.
You may play the sound as soon as the ball touches the brick & put
this code after that.
The last three parameters (false, 0, true) set the motion finish listener to be garbage collected.
How I would do it would be to first create a variable hit:Boolean and set it to true when it gets hit and change your code inside enterFrameEvents function to something like this
if(!hit && this.hitTestObject(_root.Ball)){
hit = true;
_root.ballYSpeed *= -1;
//this.parent.removeChild(this);
//removeEventListener(Event.ENTER_FRAME, enterFrameEvents);
}
if(hit){
this.alpha -= 0.1; //change value to preference
if(this.alpha <= 0){
this.parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME, enterFrameEvents);
}
}

TypeError: Error #2007: Parameter hitTestObject must be non-null

I've a question here.
I'm doing a game, space invader.
It prompt me an error
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at Enemy/eFrame()
I hope that anyone of you could help me with it.
Thank you very much!
//This is the basic skeleton that all classes must have
package{
//we have to import certain display objects and events
import flash.display.MovieClip;
import flash.events.*;
//this just means that Enemy will act like a MovieClip
public class Enemy extends MovieClip{
//VARIABLES
//this will act as the root of the document
//so we can easily reference it within the class
private var _root:Object;
//how quickly the enemy will move
private var speed:int = 5;
//this function will run every time the Bullet is added
//to the stage
public function Enemy(){
//adding events to this class
//functions that will run only when the MC is added
addEventListener(Event.ADDED, beginClass);
//functions that will run on enter frame
addEventListener(Event.ENTER_FRAME, eFrame);
}
private function beginClass(event:Event):void{
_root = MovieClip(root);
}
private function eFrame(event:Event):void{
//moving the bullet up screen
y += speed;
//making the bullet be removed if it goes off stage
if(this.y > stage.stageHeight){
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.removeChild(this);
}
//checking if it is touching any bullets
//we will have to run a for loop because there will be multiple bullets
for(var i:int = 0;i<_root.bulletContainer.numChildren;i++){
//numChildren is just the amount of movieclips within
//the bulletContainer.
//we define a variable that will be the bullet that we are currently
//hit testing.
var bulletTarget:MovieClip = _root.bulletContainer.getChildAt(i);
//now we hit test
if(hitTestObject(bulletTarget)){
//remove this from the stage if it touches a bullet
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.removeChild(this);
//also remove the bullet and its listeners
_root.bulletContainer.removeChild(bulletTarget);
bulletTarget.removeListeners();
//up the score
_root.score += 5;
}
}
//hit testing with the user
if(hitTestObject(_root.mcMain)){
//losing the game
_root.gameOver = true;
_root.gotoAndStop('lose');
}
if(_root.gameOver){
removeEventListener(Event.ENTER_FRAME, eFrame);
this.parent.removeChild(this);
}
}
public function removeListeners():void{
this.removeEventListener(Event.ENTER_FRAME, eFrame);
}
}
}
The error means you have called hitTestObject(null) in your code, which is invalid.
From the code you shared, either bulletTarget or _root.mcMain could be the reason.

Bullets will only fire to the right?

I'm making a flash game for my course at college, I have been following a tutorial but been spinning it off for my own sake. One wall I hit is that when I fire a bullet, it will only fire to the right, with a little movement up or down, I have been trying to fix it for a while but nothing is happening and nothing works.
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Wizard extends MovieClip {
private var dx:Number;
private var dy:Number;
private var Bulletspeed:int;
public var Angle:Number;
public var newAngle:Number;
var shotCoolDown:int;
const MAX_COOLDOWN=20;
public function Wizard() {
//constructor
//Shot cool down
shotCoolDown=MAX_COOLDOWN;
addEventListener(Event.ENTER_FRAME, update);
//set up an event listener for when the turret is added to stage
addEventListener(Event.ADDED_TO_STAGE, initialise);
}
function initialise(e:Event) {
//reduce shot cool down by one
shotCoolDown=shotCoolDown-1;
//add a click listener to the stage
stage.addEventListener(MouseEvent.CLICK, fire);
}
function fire(m:MouseEvent) {
//Able to shoot
if (shotCoolDown<=0) {
//resets cool down
shotCoolDown=MAX_COOLDOWN;
//spawn bullet
var B = new Bullet();
//set position and rotation of the bullet
B.rotation=rotation;
B.x=x;
B.y=y;
//add the bullet the the wizard
parent.addChild(B);
}
}
function update():void {
//Shot cool down
shotCoolDown--;
//Make the Wizard face the mouse
if (parent!=null) {
dx=stage.mouseX-this.x;
dy=stage.mouseY-this.y;
Math.abs(dx);
Math.abs(dy);
var Angle=Math.atan2(dy,dx);
var newAngle = Angle * (180 / Math.PI);
if ((0 < newAngle) && (newAngle <= 90)) {
gotoAndPlay("Right");
} else if ((90 < newAngle) && (newAngle <= 180)) {
gotoAndPlay("Down");
} else if ((-180 < newAngle) && (newAngle <= -90)) {
gotoAndPlay("Left");
} else if ((-90 < newAngle) && (newAngle <= 0)) {
gotoAndPlay("Up");
}
this.rotation=Angle;
}
}
}
}
That's the code for my player class, with things such as bullets firing and what not. I think I know the problem, I need to link it to the rest of the Wizard update. But I don't know how, here is my bullet class if needed.
package {
import flash.display.Sprite;
import flash.events.Event;
public class Bullet extends Sprite {
private var speed:int;
private var myCharacter:Wizard;
public function Bullet() {
//constructor
speed = 10;
addEventListener(Event.ENTER_FRAME, update);
}
function update (e:Event) {
//Move in the direction the bullet is facing
x=x+Math.cos(rotation/180*Math.PI)*speed;
y=y+Math.sin(rotation/180*Math.PI)*speed;
//Clears bullet once it leaves the stage
if (x<0 || x>500 || y<0 || y>500) {
//removes the update listner
removeEventListener(Event.ENTER_FRAME, update);
parent.removeChild(this);
}
}
}
}
You're setting the Wizard's rotation to Angle, which is in radians; the rotation is then passed on to the Bullet, which expects its rotation to be in degrees. It's probably better to set this.rotation=newAngle; at the end of update(), as the UIComponent class expects that value in degrees and uses it for rotating its drawing.