How to make test for collision between different objects
Copy link to clipboard
Copied
So I'm working on a program that has a object in the middle of the stage. The background moves with the left and right buttons and there are other objects i plan to make invisible so that when the object in the middle hits the invisible objects, the background changes as do the invisible objects so it seems as if the middle object when to a different room. I know there are probably better ways of doing it but so far this is the only way i have been able to teach myself and now I'm stumped.
This is the main class
package AS {
import flash.display.MovieClip;
public class Main extends MovieClip {
private var background:BackgroundDemo;//the actual map with no boundries
private var changePointOne:ChangePoint;//these will change backgrounds and locations when hit
private var hero:Hero;//test for hit detection
public function Main() {
Constants.stageRef = stage;
changePointOne = new ChangePoint;
changePointOne.x = 400;
changePointOne.y = 250;
background = new BackgroundDemo();
background.x = 200;
background.y = 200;
hero = new Hero();
hero.x = 250;
hero.y = 250;
stage.addChild(background);
stage.addChild(changePointOne);
stage.addChild(hero);
}
}
}
This is the middle object class
package AS {
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Hero extends MovieClip {
public function Hero() {
Constants.stageRef = stage;
trace("YES");
addEventListener(Event.ENTER_FRAME, frameListener);
}
private function frameListener(e: Event) { //will test wether or not the hero touches a Change Point
trace("Worked");
}
//}//FrameListener
} //Class
} //AS
This is the Invisible object class
package AS {
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class ChangePoint extends MovieClip {
private var moveLeft:Boolean;
private var moveRight:Boolean;
public function ChangePoint() {
moveLeft = false;
moveRight = false;
Constants.stageRef.addEventListener(KeyboardEvent.KEY_DOWN, keyDownFunction);
Constants.stageRef.addEventListener(KeyboardEvent.KEY_UP, keyUpFunction);
Constants.stageRef.addEventListener(Event.ENTER_FRAME, frameListener);
this.stop();
}
private function keyDownFunction (e:KeyboardEvent){
var key:uint = e.keyCode;
if (key == 65 || key == 37){// A / Left
moveLeft = true;
trace("Left");
}
if (key == 68 || key == 39){// D / Right
moveRight = true;
trace("Right");
}
}
private function keyUpFunction (e:KeyboardEvent){
var key:uint = e.keyCode;
if (key == 65 || key == 37){// A / Left
moveLeft = false;
}
if (key == 68 || key == 39){// D / Right
moveRight = false;
}
}
private function frameListener (e:Event){
if(moveLeft){
this.x-=5;
}
if(moveRight){
this.x+=5;
}
}
}
}
and this is the background class
package AS {
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class BackgroundDemo extends MovieClip {
private var moveLeft: Boolean;
private var moveRight: Boolean;
public function BackgroundDemo() {
moveLeft = false;
moveRight = false;
Constants.stageRef.addEventListener(KeyboardEvent.KEY_DOWN, keyDownFunction);
Constants.stageRef.addEventListener(KeyboardEvent.KEY_UP, keyUpFunction);
Constants.stageRef.addEventListener(Event.ENTER_FRAME, frameListener);
this.stop();
}
private function keyDownFunction(e: KeyboardEvent) {
var key: uint = e.keyCode;
if (key == 65 || key == 37) { // A / Left
moveLeft = true;
trace("Left");
}
if (key == 68 || key == 39) { // D / Right
moveRight = true;
trace("Right");
}
}
private function keyUpFunction(e: KeyboardEvent) {
var key: uint = e.keyCode;
if (key == 65 || key == 37) { // A / Left
moveLeft = false;
}
if (key == 68 || key == 39) { // D / Right
moveRight = false;
}
}
private function frameListener(e: Event) {
if (moveLeft) {
this.x -= 5;
}
if (moveRight) {
this.x += 5;
}
}
}
}
This is called the Constants class and this is the only way I know of having all three objects move or interact (somewhat)
package AS{
import flash.display.Stage;
public class Constants {
public static var stageRef:Stage;
public static var wallsRef:Walls;
public static var calRef:Cal;
public function Constants() {
// not sure what this does
}
}
}
I would really like to know how to test for collision between the hero and the changePoint object. The logic i have behind all of this might not be very efficient as i am very new to all of this. I plan to make the middle object have a walking animation and have up to 3-5 individual changePoints. There is going to be a function that when it detects that the hero made contact with a changePoint it changes the background to a different frame and re aligns the change points according to the background
Copy link to clipboard
Copied
You normally use the hitTestObject() method to determine when one object overlap intersects with another. If there are multiple objects then you can use an array to hold references to them and loop thru the array each time one of them moves (or using the ENTER_FRAME listener).
Copy link to clipboard
Copied
>>This is called the Constants class and this is the only way I know of having all three objects move or interact (somewhat)
I don't understand what you mean here. Your Main class has references to all necessary objects, and is right now the best place to test for collision, movement would normally be handled in the individual classes. Anyway, as Ned suggested you can simply use something like so in Main - after your addChild lines:
addEventListener(Event.ENTER_FRAME, checkCollisions);
function checkCollisions(e:Event):void
{
if(hero.hitTestObject(changePointOne)){
//react to collision
}
}
However, I'd suggest using the Collision Detection Kit if you're going to be doing any more than this... or even this. It's easy to use and very fast:
And one more thing, you'd don't need to do stage.addChild in your Main class,you can just do addChild alone. In that class, you're 'in' the base MovieClip and already on the Stage. also why you don't typically need to use 'this'.

