werer cool
werer cool

Reputation: 1

AS3 tween object not working with .hitTestObject()

I am having a major problem in my new browser app.

Okay so I made game where different cubes (squares) spawn at the top of the screen and I use the Tween class to make them go down the screen and then disappear.

However I want to detect a collision when a cube hits the player (that is also a flying cube).

I tried everything, truly everything but it does not seem to work. The problematic thing is that when I remove the "Tween" function it does detect collision with the hitTestObject method but when I add the "Tween" line collision won't be detected anymore.

It looks like this:

       function enemiesTimer (e:TimerEvent):void
  {
      newEnemy = new Enemy1();
      layer2.addChild(newEnemy);
      newEnemy.x = Math.random() * 700;
      newEnemy.y = 10;

      if (enemiesThere == 0)
      {
          enemiesThere = true;
          player.addEventListener(Event.ENTER_FRAME, collisionDetection)
      }



      var Tween1:Tween = new Tween(newEnemy, "y", null, newEnemy.y, newEnemy.y+distance, movingTime, true);
  }

And the collision detection part:

private function collisionDetection (e:Event):void
  {


      if (player.hitTestObject(newEnemy))
      {
          trace("aaa");
      }






  }

I am desperate for some information/help on the topic, it's been bugging me for days.

Thanks for your time, I would be very happy if someone could help me out^^

Upvotes: 0

Views: 134

Answers (2)

inverse
inverse

Reputation: 358

First, make sure the "newEnemy" instance and the "player" instance are within the same container. If they are not, their coordinate systems might not match up and could be the source of your problem.

Otherwise, you need to keep a reference to each enemy instance you create. It looks like you are only checking against a single "newEnemy" variable which is being overwritten every time you create a new enemy. This might be why you can successfully detect collision between the player and the most recent "enemy" instance.

So... you need a list of the enemies, you can use an Array for that.

private var enemyList:Array = [];

Every time you create an enemy, push it to the Array.

enemyList.push(newEnemy);

In your "collisionDetection" function, you need to loop through all of the enemies and check if the player is touching any of them.

for(var i:int = 0; i < enemyList.length; i++)
{
    var enemy = enemies[i];
    if (player.hitTestObject(enemy))
    {
        trace("Collision Detected!");
        enemy.parent.removeChild(enemy); // remove the enemy from the stage
        enemies.splice(i, 1); // remove the enemy from the list
    }
}

Upvotes: 1

user3603310
user3603310

Reputation:

I'd suggest that you move to TweenMax, it just might solve your problem, and in my experience it's much better in every possible way.

Scroll down the following page to see a few variations of this library, I myself use TweenNano, they're completely free of charge: https://greensock.com/gsap-as

I think some plugins cost money, but I doubt you'll ever need them.

Upvotes: 0

Related Questions