Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Collision detect not working

Explorer ,
Oct 30, 2015 Oct 30, 2015

I have created a Class in code called Battery and am adding it inside a function named 'Clone'. Battery is derived from a base class with a filename of Components.as. Here's where I am adding battery inside a function named 'clone':

var ComponentRef:Class = getDefinitionByName("Battery") as Class;

  var instance:object = new ComponentRef();

  addChild(DisplayObject(instance));

  instance.x = 500;

  instance.y = 300;

  instance.compName = "Battery";

  instance.amperage = 20;

  instance.partNumber = "BA12345";

I also have a movie clip with an instance name of 'negativeProbe' that I add an event listener to:

negativeProbe.addEventListener(Event.ENTER_FRAME, detectedColl);

this brings me to the detectedColl function where I am trying to do the collision detection:

var battery:Battery = new Battery();

  if (battery.hitTestObject(negativeProbe)) {

  trace("Collision!!!");

Trace never registers a hit. I don't get an error, but no hit is ever registered. What am I doing wrong here?

TOPICS
ActionScript
474
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Oct 30, 2015 Oct 30, 2015

Here you've added the object "instance" to the stage

  addChild(DisplayObject(instance));

but this object not added!

var battery:Battery = new Battery();

so you have an object in name "battery" but it's not on the stage and it'll never hit another object unless you add it.

Translate
Enthusiast ,
Oct 30, 2015 Oct 30, 2015

Try to change the variable name "instance" make it "battery"

or change the "battery" in if statement to "instance":

if (instance.hitTestObject(negativeProbe)) {

  trace("Collision!!!");

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 30, 2015 Oct 30, 2015

If I change instance to Battery I get this error:

Access of possibly undefined property x through a reference with static type Class.


If I change battery in the if statement to instance I get an error saying instance wasn't found or referenced, so I added 'instance' to the previous line:


var instance:Battery = new Battery();

  if (instance.hitTestObject(negativeProbe)) {


No error but still doesn't detect a collision. I'm stumped on this one. 😕

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 30, 2015 Oct 30, 2015

Here you've added the object "instance" to the stage

  addChild(DisplayObject(instance));

but this object not added!

var battery:Battery = new Battery();

so you have an object in name "battery" but it's not on the stage and it'll never hit another object unless you add it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 30, 2015 Oct 30, 2015

Ohhhh I think I see... I need to do an addChild on battery... I'll try that now.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 30, 2015 Oct 30, 2015

That did work. Thank you again!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 30, 2015 Oct 30, 2015
LATEST

You're welcome my friend.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines