Error #1010 in as3 when detecting collision - actionscript-3

im getting a TypeError: Error #1010: A term is undefined and has no properties.
at Defender/checkcollision()
at Defender/gameloop()
plz tell me how to solve this one.
ill give u the as3 code
public function startDefender() {
gamelevel = 3; //need some improvements here;
isfiring = true;
gunposx = gun.x;
gunposy = gun.y;
bullets = new Array();
vehicles = new Array();
missiles = new Array();
health = 100;
hits = 0;
desiredhits = 10;
_healthmeter._healthbar.width *= health / 100;
gun.startGun();
if(gamelevel != 1){
setinterceptor();
startinterceptortimer();
}
setvehicletimer();
addEventListener(Event.ENTER_FRAME, gameloop);
}
public function checkcollision(){ //checks for any collision
//COLLISION: bullets and vehicles
if((bullets.length != 0) && (vehicles.length != 0)){
for(var _b:int = bullets.length - 1; _b >= 0; _b--){
for(var _v:int = vehicles.length - 1; _v >= 0; _v--){
if(bullets[_b].hitTestObject(vehicles[_v])){
trace("Sucks");
bullets[_b].remove();
vehicles[_v].remove();
//increase the score and update scoremeter and check for desired hits
}
}
}
}
when the bullets hit the vehicles im getting this error
TypeError: Error #1010: A term is undefined and has no properties.
at Defender/checkcollision()
at Defender/gameloop()
plz help me...

if(bullets[_b].hitTestObject(vehicles[_v])){
trace("Sucks");
bullets[_b].remove();
vehicles[_v].remove();
return;

Related

Compile-time constant error Actionscript 3

I am currently trying to put together a platformer game similar to doodlejump however I am getting two errors that I am unsure of how to fix. Below is the code that I am currently using. My errors are;
**Scene 1, Layer 'Code', Frame 4, Line 181 1046: Type was not found or was not a compile-time constant: stick.
Scene 1, Layer 'Code', Frame 4, Line 207 1180: Call to a possibly undefined method stick.**
var tempStick:stick;
var tmpMc:MovieClip;
stop();
// MONITOR THE ACCELEROMETER
var myAcc:Accelerometer = new Accelerometer();
myAcc.addEventListener(AccelerometerEvent.UPDATE, onAccUpdate);
function onAccUpdate(evt:AccelerometerEvent):void{
accX = evt.accelerationX;
}
//MONITOR THE ENTER_FRAME EVENT
stage.addEventListener(Event.ENTER_FRAME, onMyEnterFrame);
//INIT STAGE WITH CLOUDS
if (firstPass == 1){
liveScore = 0;
accX = 0;
for (var i:int=0; i< 5; i++){
tempStick = new stick;
tempStick.x = Math.random()*stage.stageWidth;
tempStick.y = 0 + i*stage.stageHeight/6;
myVect[i] = tempStick;
addChild(tempStick);
tempStick.cacheAsBitmap = true;
}
firstPass = 2;
}
function onMyEnterFrame(evt:Event):void{
//MOVE X DEPENDING ON THE ACCELEROMETER
MyIcare.x -= (MyIcare.x - (MyIcare.x + accX * 10))*0.6;
//MOVE HEAD TO THE LEFT OR TO THE RIGHT
if(accX > 0) {
MyIcare.gotoAndStop(2);
}else{
MyIcare.gotoAndStop(1);
}
// Vertical speed OF THE ICARE/ANGEL
vVelocity += vAcceleration;
if((MyIcare.y > middleScreen) && (vVelocity < 0)){
// ICARE IS GOING UP
MyIcare.y += vVelocity;
}else{
if(vVelocity > 0){
// ICARE IS GOING DOWN
MyIcare.y += vVelocity;
// TEST IF ICARE TOUCHES A CLOUD
for (var i:int=0; i< 5; i++){
tmpMc = myVect[i];
if (MyIcare.hitTestObject(tmpMc))
{
vVelocity = -20;
}
}
}else{
// THE WORLD IS GOING DOWN
// WHEN ICARE IS IN THE MIDDLE OF THE SCREEN
for (var j:int=0; j< 5; j++){
tmpMc = myVect[j];
tmpMc.y -= vVelocity;
}
liveScore += 5;
}
}
// CHECK IF THE CLOUDS ARE OUT OF THE SCREEN
if(myVect[0] != null){
for (var k:int=0; k< 5; k++){
tmpMc = myVect[k];
if(tmpMc.y > stage.stageHeight){
tmpMc.y = -5;
tmpMc.x = Math.random()*stage.stageWidth;
}
}
}
//ICARE IS OUT OF THE SCREEN
//PAUSE GAME
MyIcare.y = -300;
vVelocity = 0;
vAcceleration = 0;
// PLAY FUNNY SOUND
}
Most likely your problem is you haven't told your library object (stick) to be accessible to code.
To do that, open your library panel in FlashPro, right click your stick asset/movieClip, and choose properties.
On the Symbol Properties Window, check the Export For ActionScript box. Then enter stick as in the Class field.
Now you can instantiate a stick through code.

Flash As3 Variable Issue

I have a variable on frame 1 that starts at 3000 and keeps decreasing. This variable determines the gap of time between the spawn of an object. so basically the objects spawn quicker and quicker as the this number decreases. Everything works fine except the fact when i make the character die and the main timeline goes to frame 2 to show the death screen. When clicked "play again" on that frame that makes you come back to frame 1. the objects keep spawning at whatever speed they were spawning before death. Even though the variable resets to 3000 when "play again" clicked..
I traced the variable and it does truely reset. but for some reason those spawning objects follow the previous variable.
Heres the code on the first Frame:
import flash.events.Event;
stop();
var num1:Number = 0;
var scrollSpeed:Number = 3000;
var playerState:Number = 1;
var alive:Boolean = true;
var Redbars:Array = new Array();
var Bluebars:Array = new Array();
var scoreCount:Number = 10;
addEventListener(Event.ENTER_FRAME, update);
function update(e:Event){
trace(scrollSpeed);
scoreCount += 1;
scoreText.text = (scoreCount).toString();
//SCROLL SPEED-------------------
if(scrollSpeed > 300)
{
scrollSpeed -= 1;
}
//--------------------------------
CheckForCollisionBlue();
CheckForCollisionRed();
//trace(alive);
if(player.currentFrame == 1){
playerState = 1;
}
//Check if lost
if(player.hitTestObject(leftHitBox)){
gotoAndStop(2);
scoreEnd.text = (scoreCount).toString();
}
//If dead Delete Object
if(currentFrame == 2){
deleteBlue();
deleteRed();
}
}
////
//Timer////////////////////////////////
var myTimer:Timer = new Timer(scrollSpeed,10000000);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener (e:TimerEvent):void{
//Generate Random number
num1 = randomRange();
trace(num1);
//IF NUM1 = 1------------------------------------------
if(num1 == 1){
//Create a Red Bar
var redBar = new Jump_Cube();
Redbars.push(redBar); //add enemy to the array
redBar.x = -33;
redBar.y = 99;
redBar.width = 12.5;
redBar.height = 20.45;
//Update the Bar
for(var i:int=0; i < Redbars.length; i++){
if(currentFrame == 1)
{
addChild(Redbars[i]);
}
}
}
//IF NUM1 = 2------------------------------------------
if(num1 == 2){
//Create a Blue Bar
var blueBar = new Jump_Cube2();
Bluebars.push(blueBar); //add enemy to the array
blueBar.x = -26.8;
blueBar.y = 10;
blueBar.width = 12.25;
blueBar.height = 31.90;
//Update the Bar
for(var j:int=0; j < Bluebars.length; j++){
if(currentFrame == 1)
{
addChild(Bluebars[j]);
}
}
myTimer.delay = scrollSpeed;
}
}
myTimer.start();
//--------------------------------------------------------------------
//Check for Collision------------------------------
function CheckForCollisionBlue(){
for(var i:int=0; i < Bluebars.length; i++){
if( player.hitTestObject(Bluebars[i]) ){
//Collision
trace("Didnt Hit Blue");
player.x -= 5;
player.y += 1;
}
}
}
//Check for Collision------------------------------
function CheckForCollisionRed(){
for(var i:int=0; i < Redbars.length; i++){
if( player.hitTestObject(Redbars[i]) ){
//Collision
trace("Didnt Hit Red");
player.x -= 5;
player.y += 1;
}
}
}
function randomRange():Number
{
return (Math.floor(Math.random() * (2 - 1 + 1)) + 1);
}
///////////BUTTONS------------------
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
redButton.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchEndRed);
function onTouchEndRed(e:TouchEvent):void{
player.gotoAndPlay(2);
playerState = 2;
}
blueButton.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchEndBlue);
function onTouchEndBlue(e:TouchEvent):void{
player.gotoAndPlay(19);
playerState = 3;
}
///////--CLEARING STAGE
//--------------------------------------------------------------------
//delete Blue------------------------------
function deleteBlue(){
for(var i:int=0; i < Bluebars.length; i++){
removeChild(Bluebars[i]);
}
}
//delete Red------------------------------
function deleteRed(){
for(var i:int=0; i < Redbars.length; i++){
removeChild(Redbars[i]);
}
}
Heres the code on the second frame:
stop();
alive = false;
againButton.addEventListener(TouchEvent.TOUCH_END, again);
function again(e:TouchEvent):void{
gotoAndStop(1);
playerState = 1;
scrollSpeed = 3000;
deleteBlue();
deleteRed();
}
You have to put in a removeEventListener(Event.ENTER_FRAME, update); inside your onTouchEndRed function. And probably also inside of this 'if' conditional: if(player.hitTestObject(leftHitBox)){...
Simply stop programming using frames. Use classes instead. AS3 is based on classes. It'll take you a lot of benefits. This link may help you to start: http://www.adobe.com/devnet/actionscript/getting_started.html
Additionally, FlashPlayer executes frame's code each time you jump to another frame. Thats why your variables are reinitializing.

Error #1006: removeChild is not a function

So I'm trying to create a game where in there's an object falling from the middle and you have to drag it in the left if it's good or right if it's bad.
What I'm having problems with right now is I don't know how the program would know if the object is good or bad. I think.
I'm getting an error:
Error #1006: removeChild is not a function.
I'm newbie at flash, if you have tips or whatever, please share!
http://pastebin.com/AnpN6tEy
import flash.events.Event;
var tray:Array = new Array(Legal2_1,Legal2_2,Legal2_3,Legal2_4,Legal2_5,Legal2_6,Legal2_7,Legal2_8,Legal2_9,Legal2_10,Legal2_11,Legal2_12,Legal2_13,Legal2_14,Legal2_15,Illegal2_1,Illegal2_2,Illegal2_3,Illegal2_4,Illegal2_5,Illegal2_6,Illegal2_7,Illegal2_8,Illegal2_9,Illegal2_10,Illegal2_11,Illegal2_12,Illegal2_13,Illegal2_14,Illegal2_15);
var traypos:int;
var goodpos:int;
var badpos:int;
traypos = (stage.stageWidth / 2)-100;
goodpos = ((stage.stageWidth / 3) -100);
badpos = (((stage.stageWidth/3) *2) -100);
var timerT:Timer = new Timer(1000,120);
timerT.addEventListener(TimerEvent.TIMER, traytimerhandler);
timerT.start();
var secondsT:Number = 1;
function traytimerhandler(event:TimerEvent)
{
//trace("Seconds elapsed: " + seconds);
SpawnTray(null);
secondsT++;
}
function SpawnTray(event:Event):void
{
var trayspawn:int;
trayspawn = int(Math.random() * tray.length);
var trayn:MovieClip = new tray[trayspawn]();
addChild(trayn);
trayn.x = traypos;
trayn.y = -20;
trayn.addEventListener(Event.ENTER_FRAME, MoveTray(trayspawn));
trayn.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
//trayn.addEventListener(MouseEvent.CLICK, CheckTray(trayspawn));
}
function MoveTray(trayc:int):Function
{
return function(event:Event):void {
var trayn:DisplayObject = event.target as DisplayObject;
trayn.y += 5;
if (trayn.y >= stage.stageHeight + 50)
{
CheckTray(trayc);
trayn.removeEventListener(Event.ENTER_FRAME, MoveTray);
this.removeChild(trayn);
}
}
}
function startDragging(e:MouseEvent):void
{
e.target.removeEventListener(MouseEvent.MOUSE_DOWN, startDragging);
e.target.removeEventListener(Event.ENTER_FRAME, MoveTray);
// surprise! This object will not be moved via MOUSE_DOWN,;
// because it's already being moved
// e.target.addEventListener(
e.target.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
e.target.startDrag();
}
// drag;
function stopDragging(e:MouseEvent):void
{
e.target.stopDrag();
e.target.addEventListener( Event.ENTER_FRAME, MoveTray);
e.target.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
e.target.removeEventListener(MouseEvent.MOUSE_UP,stopDragging);
return;// emergency exit. We don't need to do more
}
function CheckTray(trayspawn:int):Function
{
return function(event:Event):void {
var trayn:DisplayObject = event.target as DisplayObject;
if (trayn.x <= goodpos)
{
//good side
if (trayspawn<=14)
{
score += 15;
}
else
{
score -= 15;
}
}
if (trayn.x >= badpos)
{
//bad side
if (trayspawn<=14)
{
score -= 15;
}
else
{
score += 15;
}
}
if (trayn.x > goodpos && trayn.x < badpos)
{
//middle
score -= 15;
}
}
}
Implicit coercion error on line 37 is caused by the fact that addEventListener expects function. Change the line to:
trayn.addEventListener(Event.ENTER_FRAME, MoveTray);
"Undefined property event" problems have common cause. Event argument is missing in the signatures of the functions. They should be like this:
function MoveTray(event:Event):void
function CheckTray(event:Event):void
you can access the object that is dispatching the Event with the property target of the Event.
ex:
var m1:MovieClip = new MovieClip();
m1.name = 'm1';
m1.addEventListener( Event.ENTER_FRAME, onEnterFrame );
var m2:MovieClip = new MovieClip();
m2.name = 'm2';
m2.addEventListener( Event.ENTER_FRAME, onEnterFrame );
function onEnterFrame(e:Event):void
{
trace( 'onEnterFrame', e.target.name ); // you could see m1 and m2
}
Hope this could help you

AS 3 Error Null Object

I've got an error in my game in AS3. When my array lenght is 0 the app should go to the Menu scene. However it always appears a box saying:
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/removeChild()
at kitkat_game_fla::MainTimeline/moveBall()[kitkat_game_fla.MainTimeline::frame2:105]
I click on dismiss all and when i start the game again the ball is completely fast. What should i do. Is it because my mcBall.x is null and it shouldn´t? I already search and nothing works. Some help please.
Here's my code
stop();
var ballSpeedX:int = 23;//Velocidade em X da bola.
var ballSpeedY:int = 23;//Velocidade em Y da bola.
var mySound:Sound = new myFavSong();
var myArray:Array = new Array(mc1,mc2);
trace (myArray.length);
function iniciarCode():void{
mcPaddle.addEventListener(Event.ENTER_FRAME, movePaddle);
mcBall.addEventListener(Event.ENTER_FRAME, moveBall);
}
function movePaddle(event:Event):void{
var dx:int = mcPaddle.x - mouseX;
mcPaddle.x -= dx / 5;
if(mcPaddle.x <= mcPaddle.width/2){
mcPaddle.x = mcPaddle.width/2;
}else if(mcPaddle.x >= stage.stageWidth-mcPaddle.width/2){
mcPaddle.x = stage.stageWidth-mcPaddle.width/2;
}
}
function moveBall(event:Event):void{
mcBall.x += ballSpeedX;
mcBall.y += ballSpeedY;
if(mcBall.x <= mcBall.width/2){
mcBall.x = mcBall.width/2;
ballSpeedX *= -1;
} else if(mcBall.x >= stage.stageWidth-mcBall.width/2){
mcBall.x = stage.stageWidth-mcBall.width/2;
ballSpeedX *= -1;
}
if(mcBall.y <= mcBall.height/2){
mcBall.y = mcBall.height/2;
ballSpeedY *= -1;
} else if(mcBall.y >= stage.stageHeight-mcBall.height/2){
mcBall.y = stage.stageHeight-mcBall.height/2;
ballSpeedY *= -1;
}
if(mcBall.hitTestObject(mcPaddle)){
calcBallAngle();
}
if(mcBall.hitTestObject(mc_lettering)){
calcBallAngle();
}
if(mcBall.hitTestObject(mc1)){
removeChild(mc1);
myArray.splice(mc1, 1)
trace(myArray.length);
mc1 == null;
}
if(mcBall.hitTestObject(mc2)){
removeChild(mc2);
myArray.splice(mc2, 1);
trace(myArray.length);
mc2 == null;
}
if(myArray.length == 0){
gotoAndPlay(1,"Menu");
}
if(mcBall.hitTestObject(mcPaddle.barra_mc1)){
mcPaddle.removeChild(mcPaddle.barra_mc1);
mcPaddle.barra_mc1 == null;
mySound.play();
}
}
function calcBallAngle():void{
var ballPosition:Number = mcBall.x - mcPaddle.x;
var hitPercent:Number = (ballPosition / (mcPaddle.width - mcBall.width)) - .5;
ballSpeedX = hitPercent * 30;
ballSpeedY *= -1;
}
iniciarCode();
myArray.splice(mc2, 1); is a wrong approach, because the first parameter to splice() is an index, not an object. You should instead query indexOf() for that object, and if it's off the array already, you don't splice and don't call removeChild().
if (mc1) if (mc1.parent) // if mc1 is detached, why checking hit test?
if(mcBall.hitTestObject(mc1)){
removeChild(mc1);
myArray.splice(myArray.indexOf(mc1), 1) // at this point mc1 is still in array
trace(myArray.length);
// mc1 == null; this is wrong, unless you create a new mc1 at the start of the game
// and it should spell one equal sign, not two.
}
if (mc2) if (mc2.parent)
if(mcBall.hitTestObject(mc2)){
removeChild(mc2);
myArray.splice(myArray.indexOf(mc2), 1);
trace(myArray.length);
// mc2 == null;
}
Also such an approach defies the use of an array. Normally you don't query the individual MCs, but instead you iterate through the array and run hitTestObject against every item in the array. Like this:
for (var i:int=myArray.length-1;i>=0;i--) {
if (mcBall.hitTestObject(myArray[i])) {
removeChild(myArray[i]);
myArray.splice(i,1);
// that's all
}
}
if (myArray.length==0) { gotoAndPlay(1,"Menu"); }
Also, please get rid of "scenes", they are deprecated and can cause weird issues. Instead, use one scene and split it into frame sequences via timeline stop() calls.

Error #1009: Cannot access a property or method of a null object reference

I get the #1009 error while it visually works, can someone help me? Thanks in advance.
arrBellen is a Array on field-level.
private function bellenSpel(mv:MovieClip,x:Number):void{
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyHandler);
if(landschap.x == x){
mv.visible = true;
mv.mouseEnabled = true;
}
else{
mv.visible = false;
mv.mouseEnabled = false;
}
landschap.lblScore_onderwater.text.text = "Score: " + vogelTimer.currentCount;
if(vogelTimer.currentCount % 300 == 0) {
var bel:Bel = maakBellen();
arrBellen.push(bel);
}
for(var i = 0;arrBellen.length - 1;i++){
var bl:Bel = arrBellen[i];
bl.y += 2; // output says error is here
}
}
I think either your array isn't pospulated with the objects you think it is, or they can't cast to type Bel.
instead of:
var bel:Bel = maakBellen();
do like:
var bel:Bel = new maakBellen();
Okay here's my assumptions up front... A mgraph is right or maakBellen() is actually a function that returns an instance of Bel
you have an error in your loop condition you have:
for(var i = 0;arrBellen.length - 1;i++){
var bl:Bel = arrBellen[i];
bl.y += 2; // output says error is here
}
you should have
for(var i = 0;i < arrBellen.length;i++){
var bl:Bel = arrBellen[i];
bl.y += 2; // output says error is here
}
I'm a bit confused as to why this is causing a NPE rather than an OutOfRange type error.