View Full Version : global hittest for all objects
piratesage
November 11th, 2008, 12:13 PM
I have 5 movieclips placed on the stage, all in the same spot. I'm trying to make this hittest code have them all bump off eachother - so they never touch. when you click an object + drag it, I want it to push the other clips away.
It halfway works.. but only certain clips push others away. It seems like clips higher in the index don't get pushed away by lower ones, or visa-versa.
How can I get around that? Whatever clip is selected needs to always push the others away.
Here's the code.
import caurina.transitions.Tweener;
var i:int = 0;
var j:int = 0;
function bumper(e:Event) {
for (i=0; i<numChildren; i++) {
for (j=i+1; j<numChildren; j++) {
if (getChildAt(j).hitTestObject(getChildAt(i))) {
Tweener.addTween(getChildAt(i), {x:Math.random()*stage.stageWidth, y:Math.random()*stage.stageHeight, time:1, transition:"easeOutElastic"});
}
}
}
}
stage.addEventListener(Event.ENTER_FRAME, bumper);
for (i=0; i<numChildren; i++) {
getChildAt(i).addEventListener(MouseEvent.CLICK, dr);
}
function dr(e) {
e.currentTarget.startDrag();
}
GrndMasterFlash
November 11th, 2008, 01:32 PM
you have to rest the var for your second for loop every time otherwise j will = numChildren for every pass after the fist
so...
function bumper(e:Event) {
for (i=0; i<numChildren; i++) {
j = 0;
for (j=i+1; j<numChildren; j++) {
piratesage
November 11th, 2008, 01:44 PM
hmm - you sure? that didn't seem to do anything.
you have to rest the var for your second for loop every time otherwise j will = numChildren for every pass after the fist
so...
function bumper(e:Event) {
for (i=0; i<numChildren; i++) {
j = 0;
for (j=i+1; j<numChildren; j++) {
GrndMasterFlash
November 11th, 2008, 01:54 PM
oh and take this out
j=i+1
function bumper(e:Event) {
for (i=0; i<numChildren; i++) {
j = 0;
for (j; j<numChildren; j++) {
**edit**
also if you want to optimize your script a little it would be best to store numChildren in an int var, like:
var nc:int = numChildren
that way your not checking all the stages kids twice and on each iteration, ya dig?
piratesage
November 11th, 2008, 02:02 PM
yeah, that makes sense. thanks!
It's still not working, though. The change to the loop that you suggested just made everything go haywire. I added this stuff in, and now both object that collide move.
function bumper(e:Event) {
for (i=0; i<numChildren; i++) {
targetA = getChildAt(i);
j=0;
for (j; j<numChildren; j++) {
targetB = getChildAt(j);
if (targetA !== targetB) {
if (targetB.hitTestObject(targetA)) {
Tweener.addTween(targetA, {x:stage.stageWidth*Math.random(), y:stage.stageHeight*Math.random(), time:1});
trace(targetA.name);
}
}
}
}
}
oh and take this out
j=i+1
function bumper(e:Event) {
for (i=0; i<numChildren; i++) {
j = 0;
for (j; j<numChildren; j++) {
**edit**
also if you want to optimize your script a little it would be best to store numChildren in an int var, like:
var nc:int = numChildren
that way your not checking all the stages kids twice and on each iteration, ya dig?
GrndMasterFlash
November 11th, 2008, 02:04 PM
oh, so only one object is to move, may bad, let me think about this at lunch and i'll get back to you
***edit***
you could trying assigning a property to the item clicked, you know like, mc._isHeld = true; on click then mc._isHeld = false on release, and just check your mc._ifHeld before you continue with the tween, hope that fixes it...
piratesage
November 11th, 2008, 02:05 PM
thanks for the help. this has been driving me crazy! it seems pretty simple, but i'm not sure what's up. i'm just re-aquanting myself with actionscript after some time away from it.
oh, so only one object is to move, may bad, let me think about this at lunch and i'll get back to you
GrndMasterFlash
November 11th, 2008, 02:07 PM
i re-posted so you would come back and check my last edit
GrndMasterFlash
November 11th, 2008, 03:04 PM
hey piratesage is it working now?
piratesage
November 11th, 2008, 03:18 PM
Sort of.
I had it working for when it was clicked, but after adding some other code, it got a little more complex and I'm back at square 1.
I'm going to try a few more things and post back soon!
piratesage
November 11th, 2008, 08:56 PM
Got it.
import caurina.transitions.Tweener;
var nc:int = numChildren;
var i:int = 0;
var j:int = 0;
var isDragging = false;
var targetx;
var targety;
var obj;
for (i=0; i<nc; i++) {
getChildAt(i).addEventListener(MouseEvent.MOUSE_DO WN, drag);
getChildAt(i).addEventListener(MouseEvent.MOUSE_UP , drop);
getChildAt(i).addEventListener(Event.ENTER_FRAME, ef);
}
function drag(e:Event) {
e.currentTarget.startDrag();
isDragging = e.currentTarget;
}
function drop(e:Event) {
e.currentTarget.stopDrag();
isDragging = false;
}
function ef(e:Event) {
if (isDragging) {
for (i=0; i<nc; i++) {
if (getChildAt(i) !== isDragging) {
if (isDragging.hitTestObject(getChildAt(i))) {
targetx = getChildAt(i).x+(Math.random()*200-100);
targety = getChildAt(i).y+(Math.random()*200-100);
obj = getChildAt(i);
//i = nc;
} else {
for (j=0; j<nc; j++) {
if (getChildAt(j) !== isDragging && e.currentTarget !== getChildAt(j) && e.currentTarget.hitTestObject(getChildAt(j))) {
targetx = getChildAt(j).x+(Math.random()*200-100);
targety = getChildAt(j).y+(Math.random()*200-100);
obj = getChildAt(j);
}
}
}
}
}
} else {
for (j=0; j<nc; j++) {
if (e.currentTarget !== getChildAt(j) && e.currentTarget.hitTestObject(getChildAt(j))) {
targetx = getChildAt(j).x+(Math.random()*200-100);
targety = getChildAt(j).y+(Math.random()*200-100);
obj = getChildAt(j);
}
}
}
if (e.currentTarget.x > stage.stageWidth || e.currentTarget.y > stage.stageHeight) {
targetx = e.currentTarget.x-(Math.random()*200);
targety = e.currentTarget.y-(Math.random()*200);
obj = e.currentTarget;
}
if (e.currentTarget.x < 0 || e.currentTarget.y < 0) {
targetx = e.currentTarget.x+(Math.random()*200);
targety = e.currentTarget.y+(Math.random()*200);
obj = e.currentTarget;
}
Tweener.addTween(obj, {x:targetx, y:targety, time:1, transition:"easeOutElastic"});
}
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.