Forums

ForumsProgramming Forum

Structure for hit test object?

Posted Nov 30, '13 at 11:44am

alex57834

alex57834

89 posts

I have two classes and want stuff to happen when the fruit and the avatar collide but I don't know where to put the hit test objects



package {

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

public class avatar extends MovieClip {

public function avatar(stage):void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
}

private function myKeyDown(Event:KeyboardEvent):void
{
switch (Event.keyCode)
{
case Keyboard.LEFT:
this.x -= 5;
break;

case Keyboard.RIGHT:

this.x += 5;
break;
}
}

}

}




package {

import flash.display.MovieClip;
import flash.utils.*;
import flash.events.TimerEvent;

public class FallingObjects extends MovieClip {

public var appleArray:Array;
public var strawberryArray:Array;
public var orangeArray:Array;
public var watermelonArray:Array;
public var gameTimer:Timer;

public function FallingObjects()
{
appleArray = new Array();
strawberryArray = new Array();
orangeArray = new Array();
watermelonArray = new Array();

gameTimer = new Timer(25);
gameTimer.addEventListener(TimerEvent.TIMER, dropFruit);
gameTimer.start();
}

private function dropFruit(event:TimerEvent):void
{
if (Math.random() < 0.009)
{
var randomXApple:Number = Math.random() * 550;
var randomXStrawberry:Number = Math.random() * 550;
var randomXOrange:Number = Math.random() * 550;
var apples:apple = new apple();
var strawberrys:strawberry = new strawberry();
var oranges:orange = new orange();

apples.x = randomXApple;
apples.y = -30;
strawberrys.x = randomXStrawberry;
strawberrys.y = -30;
oranges.x = randomXOrange;
oranges.y = -30;

appleArray.push(apples);
addChild(apples);
strawberryArray.push(strawberrys);
addChild(strawberrys);
orangeArray.push(oranges);
addChild(oranges);
}

for each(apples in appleArray)
{
apples.y += 1.4;
}

for each(strawberrys in strawberryArray)
{
strawberrys.y += 2.2;
}

for each (oranges in orangeArray)
{
oranges.y++;
}

if (Math.random() < 0.003)
{
var randomXWatermelon:Number = Math.random() * 550;

var watermelons:watermelon = new watermelon();

watermelons.x = randomXWatermelon;
watermelons.y = -30;

watermelonArray.push(watermelons);
addChild(watermelons);
}

for each(watermelons in watermelonArray)
{
watermelons.y += 3.8;
}
}
}

}


I have a document class also.

 

Posted Nov 30, '13 at 2:48pm

weirdlike

weirdlike

658 posts

Knight

this is the part where I will have to test :D

instead I am going to give you a start to see if anything is working, so bear with me ;)

in your document class it looks something like this

package
{
import flash.display.*;
import flash.events.*;

public class Document extends MovieClip
{
private var hero:avatar;
private var fruitFall:FallingObjects;

function Document():void
{
hero = new avatar();
addChild(hero);

fruitFall = new FallingObjects();
addChild(fruitFall):

addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(event):void
{
//this is where the hittest will take place but lets see if we can identify the fruit
for(var i:int=0;i &lt fruitFall.numChildren;i++)
{
trace(fruitFall.getChildAt(i));
}
}
}
}

 

Posted Nov 30, '13 at 3:41pm

alex57834

alex57834

89 posts

This is the document class now I have added your stuff
it came up with the error

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Document/loop()



package {

import flash.display.MovieClip;
import flash.events.*;

public class Document extends MovieClip {

var avatarGuy:avatar;
var fallFruit:FallingObjects;

public function Document()
{
trace("Document Class Working&quot;);

var avatarGuy:avatar = new avatar(stage);
avatarGuy.x = 50;
avatarGuy.y = 267;
addChild(avatarGuy);

var fallFruit:FallingObjects = new FallingObjects();
addChild(fallFruit);

addEventListener(Event.ENTER_FRAME, loop);
}

private function loop(event):void
{
for(var i:int = 0; i < fallFruit.numChildren;i++)
{
trace(fallFruit.getChildAt(i));
}
}

}

 

Posted Nov 30, '13 at 4:07pm

weirdlike

weirdlike

658 posts

Knight

package {

import flash.display.MovieClip;
import flash.events.*;

public class Document extends MovieClip {
//declared variables here become seen throughout the class
private var avatarGuy:avatar;
private var fallFruit:FallingObjects;

public function Document()
{
trace("Document Class Working&quot;);

//declared variables here are limited to this function only
avatarGuy = new avatar(stage);
avatarGuy.x = 50;
avatarGuy.y = 267;
addChild(avatarGuy);

//change to this
fallFruit = new FallingObjects();
addChild(fallFruit);

addEventListener(Event.ENTER_FRAME, loop);
}

private function loop(event):void
{
for(var i:int = 0; i < fallFruit.numChildren;i++)
{
trace(fallFruit.getChildAt(i));
}
}

}

 

Posted Nov 30, '13 at 4:18pm

alex57834

alex57834

89 posts

still doesn't fix the problem the
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Document/loop()

thanks for pointing that out on variables aswell.

 

Posted Nov 30, '13 at 4:32pm

weirdlike

weirdlike

658 posts

Knight

that is strange to me as I tested and it worked you can also move the loop function into the document function itself remove the private and then test like that

package
{
import flash.display.*;
import flash.events.*;

public class document extends MovieClip
{
var avatarGuy:avatar;
var fallFruit:fallingObjects;

function document():void
{
avatarGuy = new avatar(stage);
avatarGuy.x = 50;
avatarGuy.y = 267;
addChild(avatarGuy);

fallFruit = new fallingObjects();
addChild(fallFruit);

addEventListener(Event.ENTER_FRAME, loop);
function loop(event):void
{
for(var i:int = 0; i < fallFruit.numChildren;i++)
{
if(avatarGuy.hitTestObject(fallFruit.getChildAt(i)))
{
trace(fallFruit.getChildAt(i));
}
}
}
}
}
}

 

Posted Nov 30, '13 at 5:11pm

alex57834

alex57834

89 posts

Yeah working thanks. How would I delete the fruit that I touched.

 

Posted Nov 30, '13 at 5:42pm

weirdlike

weirdlike

658 posts

Knight

if(avatarGuy.hitTestObject(fallFruit.getChildAt(i)))
{
fallFruit.removeChild(fallFruit.getChildAt(i));
}

 
Reply to Structure for hit test object?

You must be logged in to post a reply!