Jump to content
Search Community

hitTestObject with TweenLite ans AS3

MutleyDog test
Moderator Tag

Recommended Posts

Hi,

 

I've got a game coming on quite nicely in AS3 and it requires people to navigate objects. I'm using hitTestObject on a vew simple shapes - sometimes it returns 'hit' and othertimes not. i've simplified the code to:

import com.greensock.TweenLite;
import com.greensock.easing.*;

stage.addEventListener(MouseEvent.CLICK,ChangeMyBadgerLocation, false, 0, true);
//Stage_mc.addEventListener(MouseEvent.CLICK,ChangeMyBadgerLocation, false, 0, true);
function ChangeMyBadgerLocation(e:Event):void {
var myBadgerX:int = 0;
var myBadgerY:int = 0;
myBadgerX = stage.mouseX;
myBadgerY = stage.mouseY;
TweenLite.to(Badger,1,{x:myBadgerX, y:myBadgerY, ease:Cubic.easeInOut});
	if (Badger.hitTestObject(BIGobject)) {   
       	trace("Ouch - hit the object!");  
	}
}

Link to comment
Share on other sites

sorry - working from home and got distracted by my 2 year old daughter playing. I've got a game where the player can be a range of animals. In my simplified version they have to miss objects, by clicking on the screen and TweenLiting to a different area. The problem I'm having is sometimes they pass through the object when the hitTestObject should return trace("Ouch - that hurt!"). I've attached my .fla in case it's of interest?

Andy

PS: They really need to stop when they hit the object which I think is TweenMax function.

Link to comment
Share on other sites

You're only running the hitTestObject() when the tween begins, but it sounds like you want to run it every time your Badger changes position which can be accomplished with an onUpdate. Your code should look something like this:

 

stage.addEventListener(MouseEvent.CLICK,ChangeMyBadgerLocation, false, 0, true);
function ChangeMyBadgerLocation(e:Event):void {
TweenLite.to(Badger,1,{x:stage.mouseX, y:stage.mouseY, ease:Cubic.easeInOut, onUpdate:tweenUpdateHandler});
}

function tweenUpdateHandler():void {
if (Badger.hitTestObject(BIGobject)) {   
	trace("Ouch - hit the object!");  
}
}

Link to comment
Share on other sites

Great Stuff Jack - my output reads (on one mouse click):

Ouch - hit the object!

Ouch - hit the object!

Ouch - hit the object!

Ouch - hit the object!

Ouch - hit the object!

Ouch - hit the object!

Ouch - hit the object!

Ouch - hit the object!

Ouch - hit the object!

Ouch - hit the object!

Ouch - hit the object!

Ouch - hit the object!

Ouch - hit the object!

Ouch - hit the object!

Link to comment
Share on other sites

The only thing I might 'add' is if I use TweenMax and do:

if (Badger.hitTestObject(BIGobject)) {

trace("Ouch - hit the object!");

TweenMax.killTweensOf(Badger);

}

 

This causes the badger to gets 'embedded' into the BIGobject and it takes several clicks to un-stick him. I think the way round this is to set Boolean to overwrite the killTweensOf and do a little bit of trigonometry to make the badger 'bounce' a small distance off the big object. Also, as the shapes in the game are irregular I believe I should make several smaller hit objects to mark out the boundary - this is how it's done is AS2.

Link to comment
Share on other sites

Yeah, to really do it right, you need to do the math and figure out exactly where the object's boundaries touch the other object's boundaries along the trajectory path. Another (less efficient and accurate) way of doing it would be to back the tween up incrementally until the object is no longer hitting the other one (like reduce the tween's currentTime property by 0.01 over and over again, doing a hitTestObject() until it returns false). Not that I'd advise that, but it's an option.

Link to comment
Share on other sites

Well, I’ve attached a file which has the badger moving and managing to ‘bounce’ off irregular shaped objects. Hope it’s of use to someone.

 

... there’s one small snag though, and after a few hours it’s either complex – or I’m missing something. I need to pass a variable to the ‘tweenUpdateHandler’ so that I can tell it which direction the badger is moving to ‘bounce’ it the opposite direction (i.e. off the object it’s just hit). There are 8 possible directions (up, down, left, right and four diagonals). I’ve added the value ‘direction’ to be parsed into the tweenUpdateHandler ('direction' is an integer going from 1 to 8 representing 8 directions the badger could be moving in) – once parsed into the tweenUpdateHandler it stops the killTweensOf from working.

 

My code is:

stop();
import com.greensock.*;
import com.greensock.easing.*;


stage.addEventListener(MouseEvent.CLICK,ChangeMyBadgerLocation, false, 0, true);

function ChangeMyBadgerLocation(e:Event):void {
var direction:int = 6;	// quick fudge as the code for getting the direction and 'turning' the badger is quite long - happy to submit it if intersted?
TweenMax.to(Badger,1,{x:stage.mouseX, y:stage.mouseY,ease:Sine.easeInOut, onUpdate:tweenUpdateHandler(direction)});
}

function tweenUpdateHandler(direction):void {
  if (Badger.hitTestObject(BIGobject1)||Badger.hitTestObject(BIGobject2)||Badger.hitTestObject(BIGobject3)||) {   
     trace("Ouch - hit the object!"); 
  TweenMax.killTweensOf(Badger); //this doesnt work once I've parsed the 'direction' variable in
  Badger.x = Badger.x - 20;	//nudge the badger a little to the left - off the object it's hit. But this could be any one of 8 directions so I need to parse the direction variable.
  }
}

Link to comment
Share on other sites

You're using the wrong syntax.

 

BAD: {onUpdate:tweenUpdateHandler(direction)}

GOOD: {onUpdate:tweenUpdateHandler, onUpdateParams:[direction]}

 

Whenever you do tweenUpdateHandler(direction), it tells Flash to literally immediately call that function and use the result as the onUpdate. :)

Link to comment
Share on other sites

Great stuff. I've attached my final .fla which contains all the solutions to the questions raised in this thread. Thanks for the help and support

MutleyDog :idea:

 

Keywords:

hitTestObject, AS3, TweenMax, pass variables, character rotation

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...