Forums → Programming Forum → Structure for hit test object?
7 | 5728 |
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 orangesrange = 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.
- 7 Replies
this is the part where I will have to test
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 < fruitFall.numChildren;i++)
{
trace(fruitFall.getChildAt(i));
}
}
}
}
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"
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));
}
}
}
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"
//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));
}
}
}
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.
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));
}
}
}
}
}
}
if(avatarGuy.hitTestObject(fallFruit.getChildAt(i)))
{
fallFruit.removeChild(fallFruit.getChildAt(i));
}
You must be logged in to post a reply!