Copy link to clipboard
Copied
Hello. I have my character on the stage and the code controlling my character in the document class. In the document class i have a function that dynamically spawns platforms that come down the screen by loading my Platforms.as which has all the code for the random spawning locations and whatnot. How can i create a collision detection between these dynamic platforms and my character?
Copy link to clipboard
Copied
If you already have access to the character in the document class, and you create the platforms in the document class, then all you should need to is is continuously execute character.hitTestObject(platforms). You can place the platforms in an array and loop thru the array to check when a collision occurs.
Copy link to clipboard
Copied
thankyou for your reply. ok so ive put my platform code into my document class but it doesnt work because i had a function in the document class to call them to the stage it went like this
var enemy = new Platforms();
stage.addChildAt(enemy, 0);
that would call the platforms class and reference it as enemy then spawn an enemy every time the function was called. how do i do this within the class?
Copy link to clipboard
Copied
You probably do not need to include the stage reference.
How does this question relate to the question in your first posting?
Copy link to clipboard
Copied
It relates to the first question because it worked when they were in seperate classes (except for hit detection because it was unreferenceable) but now i cannot call it in the same class because the way i was spawning them was using
var enemy = new Platforms();
stage.addChildAt(enemy, 0);
but that uses the Platforms class and i was wondering how i could get it to use the code in the same class instead of the Platforms class.
Also not sure what you mean with your first sentence.
Copy link to clipboard
Copied
If you are creating the Platforms objects in the same file where you control the character then you have everything you need in that one file to target the objects. When you create that enemy object in the document class you have a direct line to target it as a result. If you store that in an array then you can retain that targeting even if you create another enemy object. As long as you cretain the refeence to each enemy object you create in the document class you can target them in the document class.
Copy link to clipboard
Copied
Alright thanks that makes sence but im kinda new to this. How would i implement this in my document class? at the moment it just spawns one platform and it doesnt move.
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class OddDog extends MovieClip
{
static var PlatformTimer:Timer;
var goRight:Boolean = true;
var goLeft:Boolean = false;
var speed:Number;
static var list:Array = new Array();
var shootTimer:Timer;
public function OddDog()
{
//platform.visible = false;
changer.visible = false;
char.visible = false;
tap.visible = false;
startbutton.addEventListener(MouseEvent.CLICK, startButton);
tap.addEventListener(MouseEvent.CLICK, tapevent);
char.addEventListener(Event.ENTER_FRAME , oneventfunction);
changer.addEventListener(MouseEvent.MOUSE_DOWN, mousedown);
function movetimerfunction(eventObject:Event):void
{
if(hitreg.visible == false && goLeft == true) char.x +=15;
if(hitreg.visible == false && goRight == true) char.x -=15;
}
function Platforms()
{
speed = -10
list.push(this);
this.y = 0;
this.x = Math.random() * 340 + 150
addEventListener("enterFrame", enterFrame);
var interval:Number = 50;
shootTimer = new Timer(interval);
shootTimer.start();
}
function enterFrame(e:Event)
{
this.y -=speed;
if (this.y < -100)
{
kill();
return;
}
if (this.hitTestObject(char))
{
char.gotoAndStop('right')
}
function kill()
{
removeEventListener("enterFrame", enterFrame);
stage.removeChild(this);
for (var i in list)
{
if (list == this)
{
delete list;
}
}
}
}
function mousedown(e:MouseEvent):void
{
if (goRight == true && goLeft == false)
{
goRight = false;
goLeft = true;
}
else
{
mousedown2(null);
}
}
function mousedown2(e:MouseEvent):void
{
if (goRight == false && goLeft == true)
{
goRight = true;
goLeft = false;
}
}
function hittestright(e:Event):void
{
if(char.hitTestObject(hitregright))
{
char.gotoAndStop('left');
goLeft = false;
goRight = false;
}
}
function hittestleft(e:Event):void
{
if(char.hitTestObject(hitregleft))
{
char.gotoAndStop('right');
goLeft = false;
goRight = false;
}
}
function oneventfunction(e:Event):void
{
movetimerfunction(null);
hittestright(null);
hittestleft(null);
}
}
private function startButton(event:MouseEvent):void
{
char.gotoAndStop('idle');
char.visible = true;
tap.visible = true;
startbutton.visible = false;
odddogsplash.visible = false;
}
private function tapevent(event:MouseEvent):void
{
PlatformTimer = new Timer(1500);
PlatformTimer.addEventListener("timer", sendEnemy);
PlatformTimer.start();
hitreg.visible = false;
changer.visible = true;
tap.visible = false;
}
function sendEnemy(e:Event)
{
var enemy = new Platforms();
Platforms(null);
stage.addChildAt(enemy, 0);
}
}
}
Copy link to clipboard
Copied
Can you clarify some things for me... does your Platforms class create a single instance of a Platforms (one platform thingy)? I get the impression that your timer is creating a new one of them every 1.5 seconds based on the code you show just above between your tapevent and sendEnemy functions.
If so, is each Platforms (enemy) object what you want to perform a hitTest against the character?
Just in case it matters, when you use addChildAt(enemy, 0) you are sending the newly created enemy object to the bottom of the disolay list, meaning it will be sitting behind any other content that you have on the stage... just in case you expect to see more of them. If these enemies are not moving and they are supposed to be, they are likely stacking up one atop the other and appear to be just one.
Copy link to clipboard
Copied
Yes it calls a new one every 1.5 seconds and yes every platform i want to perform a hittest on the character because the platforms have gaps between them which the character must go through so i want it to be able to do something if one of these are hit. And no it doesnt matter if they are behind because they dont overlap and nothing is behind or infront of them, but is there a way i can put them on top so i can add a background texture? And when its in the one class only one appears and its always in the same spot and i dont think the randomiser for positions is working. probably because my sendEnemy function is still using the Platforms class. Im not sure if they are stacking actually because there is a transparent piece between it which isnt filled but the randomiser isnt working so they might be ontop of eachother. This is what my Platforms class contains at the moment. All the guts were moved to the document class.
package
{
import flash.display.DisplayObject;
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.Timer;
public class Platforms extends MovieClip
{
}
}
And no they are not moving at all
Copy link to clipboard
Copied
It looks as if you pulled all of the code out of the Platforms class and just stuck it into your document class thinking it would still consider "this" to be the platform. When yo refer to "this" in the dicument class you are basically refering to whatever object contains the timeline/code, which will be the main timeline in this case. That will explain why nothing is moving. You should probably keep the code thatcontrols the Platforms inside the Platforms class.
If you want to have the Platforms sit above a background, then create a background and make sure it occupies the 0 index position. When you addChildAt for the platforms, do not place them at the 0 index position like you do.
I don't think you are ready to solve the issue of hitTesting the Platforms at this point. You should work out the issues you have with getting them randomly placed and animated first. Try to solve one problem at a time.
Copy link to clipboard
Copied
They randomly place and are animated fine when they are in the seperate files i just want help putting them in the one file so i can hittest them. I just noticed how i reference this instead and thats really silly but how would i go by making it work? And yea with the indexes i was going to put 1 when i made my background but i put it at 0 so it would be behind my walls which are infront of it. I did basically just paste it in there not knowing how to properly implement it.
Copy link to clipboard
Copied
package
{
import flash.display.DisplayObject;
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.Timer;
public class OddDog extends MovieClip
{
var platformref = new Platforms();
static var PlatformTimer:Timer;
var goRight:Boolean = true;
var goLeft:Boolean = false;
var speed:Number;
static var list:Array = new Array();
var shootTimer:Timer;
public function OddDog()
{
//platform.visible = false;
changer.visible = false;
char.visible = false;
tap.visible = false;
startbutton.addEventListener(MouseEvent.CLICK, startButton);
tap.addEventListener(MouseEvent.CLICK, tapevent);
char.addEventListener(Event.ENTER_FRAME , oneventfunction);
changer.addEventListener(MouseEvent.MOUSE_DOWN, mousedown);
function movetimerfunction(eventObject:Event):void
{
if(hitreg.visible == false && goLeft == true) char.x +=15;
if(hitreg.visible == false && goRight == true) char.x -=15;
}
function mousedown(e:MouseEvent):void
{
if (goRight == true && goLeft == false)
{
goRight = false;
goLeft = true;
}
else
{
mousedown2(null);
}
}
function mousedown2(e:MouseEvent):void
{
if (goRight == false && goLeft == true)
{
goRight = true;
goLeft = false;
}
}
function hittestright(e:Event):void
{
if(char.hitTestObject(hitregright))
{
char.gotoAndStop('left');
goLeft = false;
goRight = false;
}
}
function hittestleft(e:Event):void
{
if(char.hitTestObject(hitregleft))
{
char.gotoAndStop('right');
goLeft = false;
goRight = false;
}
}
function oneventfunction(e:Event):void
{
movetimerfunction(null);
hittestright(null);
hittestleft(null);
}
}
private function startButton(event:MouseEvent):void
{
char.gotoAndStop('idle');
char.visible = true;
tap.visible = true;
startbutton.visible = false;
odddogsplash.visible = false;
}
private function tapevent(event:MouseEvent):void
{
PlatformTimer = new Timer(500);
PlatformTimer.addEventListener("timer", sendEnemy);
PlatformTimer.start();
hitreg.visible = false;
changer.visible = true;
tap.visible = false;
}
function sendEnemy(e:Event)
{
Platformfunction(null);
stage.addChildAt(platformref, 0);
}
function Platformfunction(e:Event):void
{
speed = -10
list.push(platformref);
platformref.y = 0;
platformref.x = Math.random() * 340 + 150
addEventListener("enterFrame", enterFrame);
var interval:Number = 50;
shootTimer = new Timer(interval);
shootTimer.start();
}
function enterFrame(e:Event)
{
platformref.y -=speed;
if (platformref.y < -100)
{
kill();
return;
}
if (platformref.hitTestObject(char))
{
char.gotoAndStop('right')
goLeft = false;
goRight = false;
}
function kill()
{
removeEventListener("enterFrame", enterFrame);
stage.removeChild(platformref);
for (var i in list)
{
if (list == this)
{
delete list;
}
}
}
}
}
}
So ive updated it and now i have this and they spawn and hittest fine but every time a new platform spawns the previous is removed. any ideas?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now