HitTest Not Working...
Hi! Im having a problem with hitTest... I wrote this:
Code: onClipEvent(enterFrame){ if(this.hitTest(_root.light)){ _root.gotoAndPlay("menu") } }
I think maybe the reason is that the mc "light" moves inside it... And i put the code on the main "light"... But i really dont know...Anyone see anything wrong in the script? ("menu" is on the same timeline btw)
FlashKit > Flash Help > Flash ActionScript
Posted on: 08-25-2006, 11:05 AM
View Complete Forum Thread with Replies
See Related Forum Messages: Follow the Links Below to View Complete Thread
HitTest Not Working
any thoughts on why the hitTest is not functioning. thanks
onClipEvent(load) {
_root.initGame();
}
onClipEvent(enterFrame) {
_root.newPlane();
_root.movePlanes();
_root.movePlane();
// set the cursor
_root["cursor"]._x = _root._xmouse;
_root["cursor"]._y = _root._ymouse;
}
onClipEvent (mouseDown) {
// show blast
_root["cursor"].gotoAndPlay("blast");
// get the location of the click
x = _root._root["cursor"]._x;
y = _root._root["cursor"]._y;
// loop through all planes to see which was clicked
for (i=0; i<10; i++) {
// see if this was the one clicked
if (_root["plane"+i].hitTest(x, y, false)) {
// have the clip play the "hit" animation
gotoAndPlay(2);
// increase score and show it
score++;
}
}
}
function initGame() {
// init plane variables
nextPlaneTime = 0;
nextPlane = 0;
planes = [];
numPlanes = 10;
}
function newPlane() {
// more planes?
if (nextPlane < numPlanes) {
// time for next?
if (getTimer() > nextPlaneTime) {
// create new plane clip
attachMovie("plane", "plane"+nextPlane, nextPlane);
_root["plane"+nextPlane]._x = -30;
// choose height of plane
_root["plane"+nextPlane]._y = int(Math.Random()*100)+20;
// add plane to array
planes.push({clip: "plane"+nextPlane, d: dx});
// set things up for next plane
nextPlane++;
nextPlaneTime = getTimer() + 2000;
}
}
}
function movePlanes() {
// loop through planes in array
for(i=planes.length-1;i>=0;i--) {
// get speed and clip
dx = planes[i].d;
plane = _root[planes[i].clip];
// move plane AT SPEED OF 10
plane._x += 10;
// plane exit right
} if ((dx > 0) and (plane._x > 570)) {
plane.removeMovieClip();
planes.splice(i,1);
}
}
stop();
Hittest Not Working
I have a MC that can be dragged around a stage; I want limits on where it can be dragged to. I created this with flash 5 and have now moved to MX04pro. It worked great in 5 but now none of my hittest work with MX. any ideas.
objects: b1,b2,b3,b4 are my boarders left, right, bottom and top.
the below code is from the dragable MC.
please forgive if the programming is rough since im just learning. if you have a more efficient way of doing this let me know.
w = getProperty(this, _width);
h = getProperty(this, _height);
// setting boundries
// checking left
if ((this.hittest(_level0.b1))) {
setProperty(this, _x, 9);
// checking top
} else if ((this.hittest(_level0.b2))) {
setProperty(this, _y, 8);
// checking right
} else if ((this.hittest(_level0.b3))) {
setProperty(this, _x, 743-w);
// checking bottom
} else if ((this.hittest(_level0.b4))) {
setProperty(this, _y, 433-h);
}
gotoAndPlay(1);
HitTest Isn't Working...
why isn't this working?:
if(eskimo.hitTest(line)){
gotoAndStop("Scene 2", 1);
}
(I am using flash mx 2004 pro)
Why Isn't The HitTest Working?
To start, I'm using Flash MX (not 2004). I have to make a minigame using the hitTest function for a class, but the god-damn hitTest will not work. In my case, it's Red Bull goring humans. However, the collision detection isn't working. The two just fly right through each other. I've tried everything I can think of (and that I know of). I'm not sure what the problem with my code is, but I'll put both codes down and maybe see if anyone can help me out here. The code for the bull is this:
onClipEvent (load) {
speed = 12
}
onClipEvent (enterFrame) {
if (Key.isDown(key.right)){
this._x += speed;
}
if (Key.isDown(key.left)) {
this._x -= speed;
}
if (Key.isDown(key.down)) {
this._x -= speed*Math.sin(this._rotation*(Math.PI/180));
this._y += speed*Math.cos(this._rotation*(Math.PI/180));
}
if (Key.isDown(key.up)) {
this._x += speed*Math.sin(this._rotation*(Math.PI/180));
this._y -= speed*Math.cos(this._rotation*(Math.PI/180));
}
}
onClipEvent (enterFrame) {
if (this.hitTest(_root.Monkey3)){
_root.count ++;
_root.Monkey1.swapDepths(10);
_root.removeMovieClip()
}
if (this.hitTest(_root.Monkey3)){
_root.gotoAndStop(2);
}
}
And the code for the human is as follows:
onClipEvent (load) {
this._x = Math.random()*717+40;
this._y = Math.random()*493+67;
this._xscale = Math.random()*65+10;
this._yscale = Math.random()*65+10;
speedX = Math.random()*9+1;
speedY = Math.random()*9+1;
}
onClipEvent (enterFrame) {
this._x += speedX;
this._y += speedY;
if (this._x > 757) {
speedX = -speedX;
} else if (this._x < 75) {
speedX = -speedX;
}
if (this._y > 560) {
speedY = -speedY;
} else if (this._y < 75) {
speedY = -speedY;
}
}
I honestly don't know what I'm doing wrong, but since this project is due tomorrow, I urgently need some help, and as such, any help would be GREATLY appreciated. For reference, the controls are working fine.
HitTest Not Working
Can anyone see why this hitTest isn't working? What I'm trying to do is develop a hitTest so that if any of the scrambled letters hit any of the letters in the correct order, something happens.
code:
function placeTargetClips (){
var len:Number = shuffledWord.length;
var y_offset:Number = 470 + len * 25;
var parent_mc:MovieClip = this.createEmptyMovieClip("correctOrderHolder_mc", this.getNextHighestDepth());
var scrambledParent_mc:MovieClip = this.createEmptyMovieClip("scrambledOrderHolder_mc ", this.getNextHighestDepth());
for(var i:Number = 0; i < len; i++){
var currentLetter:String = wordCorrectOrder.charAt(i);
var correctOrderLetter:String = currentLetter + "_mc" + i;
var scrambledLetter:String = shuffledWord.charAt(i) + "_mc" + i;
var correctOrder_mc:MovieClip = parent_mc.attachMovie("targetLetterSY", correctOrderLetter, parent_mc.getNextHighestDepth());
var shuffled_mc:MovieClip = scrambledParent_mc.attachMovie(shuffledWord.charAt (i) + "_letterSY", scrambledLetter, scrambledParent_mc.getNextHighestDepth());
shuffled_mc.onPress = letterOnPress;
shuffled_mc.onRelease = shuffled_mc.onReleaseOutside = letterOnRelease;
//This is the line that isn't working:
if (this.scrambledParent_mc[scrambledLetter].hitTest(this.parent_mc[correctOrderLetter])) {
trace("Ouch!");
}
}
}
function letterOnPress():Void {
startDrag(this);
correct_mc._visible = false;
}
function letterOnRelease():Void {
stopDrag();
}
}
HitTest Not Working
This is the strangest thing. I've got one row of scrambled letters on top, then a row of letters in the correct order beneath that. If I drag the last scrambled letter to the last position in the correct order row first, then all of the hitTests work. However, no other combination works. Can someone see what's wrong? I'll put the pertinant parts of the code below and also attach the fla. (The hitTest is toward the bottom, in the letterOnRelease function.)
code:
function placeTargetClips (){
var len:Number = shuffledWord.length;
var y_offset:Number = 470 + len * 25;
var varOrderedParent_mc:MovieClip = this.createEmptyMovieClip("correctOrderHolder_mc", this.getNextHighestDepth());
var varScrambledOrderedParent_mc:MovieClip = this.createEmptyMovieClip("scrambledOrderHolder_mc ", this.getNextHighestDepth());
for(var i:Number = 0; i < len; i++){
var currentLetter:String = wordCorrectOrder.charAt(i);
var sCorrectOrderLetter:String = currentLetter + "_mc" + i;
var sScrambledLetter:String = shuffledWord.charAt(i) + "_mc" + i;
var correctOrder_mc:MovieClip = varOrderedParent_mc.attachMovie("targetLetterSY", sCorrectOrderLetter, varOrderedParent_mc.getNextHighestDepth(), {_y: 285, _x: 470 + (45 * i) - (shuffledWord.length * 25)});
var shuffled_mc:MovieClip = varScrambledOrderedParent_mc.attachMovie(shuffledW ord.charAt(i) + "_letterSY", sScrambledLetter, varScrambledOrderedParent_mc.getNextHighestDepth() , {_y: 175, _x: 470+(45*i) - (shuffledWord.length * 25)});
correctOrder_mc.target_letter.text = currentLetter;
shuffled_mc.onPress = letterOnPress;
shuffled_mc.onRelease = shuffled_mc.onReleaseOutside = letterOnRelease;
shuffledHT_mc = varOrderedParent_mc[sCorrectOrderLetter];
correctHT_mc = varScrambledOrderedParent_mc[sScrambledLetter];
}
}
function letterOnPress():Void {
startDrag(this);
this._parent.selected = this;
this._alpha = 50;
correct_mc._visible = false;
}
function letterOnRelease():Void {
stopDrag();
//This is the line that isn't working right:
if (shuffledHT_mc.hitTest(correctHT_mc)) {
trace("Ouch!");
}
this._parent.selected = null;
this._alpha = 100;
}
placeTargetClips ();
HitTest With X Any Not Working...
Hi..
I am trying to make a simple hitTest action and does not seem to work...
here is the script:
Code:
if(this.hitTest(_root.sol._x, _root.sol._y, true)) {
trace("bla bla");
}
I don't see anything wrong.. but.. still I may be mistaking.
the hitTest code is on the "bomb" symbol in the library..
tnx in advance.
ovidiu
HitTest Is Not Working Between Two Mc's Placed Together In One
Flash 8, AS 2.0
i have a movie clip placed on the main stage called "world". Inside of the world movie clip are, along with other things, two movie clips: enemy1, and boundry. I want to hitTest enemy1 and boundry so that I can have the enemies react to the terrain along with other things. (if you hit a wall...stop walking).
heres the code I have now, but I have tried every way of calling hitTest between the two mcs that i can think of, and no matter what it always turns out true. The problem is that it is testing the entire shape of the boundry mc, and not just the filled in areas. I usually know how to test for this, but it just isn't working for me.
class enemy1:
if(_parent.boundry.hitTest(this._x, this._y+2, true)){//checks just under the enemy for ground
nextPoint = this._x + (distance*nextDirection);//if its there, set next point
} else {
nextPoint = this._x;//otherwise dont move.
}
ive tried all the following:
_root.world.boundry.hitTest(this._x, this._y+2, true))
_root.world.boundry.hitTest(this._x, this._y+2, false))
_parent.boundry.hitTest(this._x, this._y+2, true))
this.hitTest(_root.world.boundry._x, )root.world.boundry._y+2, true))
_root.world.boundry.hitTest(this))
I literally copied the hitTest code from another movie clip that is not within world movie clip which works perfectly...so I dont know why there is a difference.
[F8 HELP] HitTest Not Working?
I am making a game, and using drag to move the character around, but when I run a hitTest on it, I want it to add 5 to the score and 1 to the variable of which, when it reaches 5, sends you to the next level.
I am running this script:
code: if (character.hitTest(object)){
score += 5
hitcount += 1
}
but it isn't working.
Help please?
[F8] HitTest Not Working
Hey guys, this project has gotten rather hefty and it involves a C# server and lots of my time, so I dont really want to upload the code. But hopefully you guys can help me.
Im creating a movie clip on the main layer (not inside a movieclip) as follows:
Code:
_root.createEmptyMovieClip("dragBox", 100000000000000);
then when the mouse is pressed and moved a blue box is drawn (a selection box such as in any RTS game)
Code:
onMouseMove = function () {
if (leftisDown)
{
dragBox.clear();
dragBox.lineStyle(1, 0x0033CC);
dragBox.moveTo(leftx, lefty);
dragBox.lineTo(_xmouse, lefty);
dragBox.lineTo(_xmouse, _ymouse);
dragBox.lineTo(leftx, _ymouse);
dragBox.lineTo(leftx, lefty);
}
}
This works fine and a box is drawn, but when the mouse button is released the following code is called to select the items:
Code:
for(var i = 0; i < itemList.length; i++)
{
if(dragBox.hitTest(itemList[i]))
{
debug.text = "box hit";
selectedList.push(itemList[i].id);
}
}
dragBox.clear();
The code gets to the for loop, but the if statement is never passed, and so no items are ever selected. I could get around this by dooing hittesting based on x and y and width and height, but that has limited functionality and I would prefer to use flashs hitTest. Can anybody see why this might not be working, or have any ideas as to where I might look it up?
Thanks,
RockSpyder
[F8] Why Is This HitTest Not Working =[
Yeah ok heres the deal my hit test isn't working can you see why:-
Code:
onClipEvent (enterFrame) {
if (this.hitTest(_root.cursor.cursor.needle)) {
trace("works");
}
}
heres the layout the highlighted symbol is where the code is on:-
http://img451.imageshack.us/my.php?i...getpathwl3.jpg
ok and one last thing "block" is being randomly duplicarted using this code on the _root frame:-
Code:
function spawn() {
if (i<200) {
duplicateMovieClip("_root.block", "block"+i, i);
_root["block"+i]._x = random(396)+50;
_root["block"+i].swapDepths(_root[""+i]);
i++;
}
}
setInterval(spawn,3000);
Please Help I need this fixed =[
[F8] HitTest Not Working
For some strange reason.. my HitTest command in the below class isn't working...
Anyone have any suggestions?
PHP Code:
class followMe {
//variable definitions
public var follow:MovieClip;
public var speed:Number;
public var ob:MovieClip;
public var frame:Function;
//constructor
function followMe(follow:MovieClip, speed:Number, ob:MovieClip, frame:Function) {
this.follow = follow;
this.speed = speed;
this.ob = ob;
this.frame = frame;
}
//public functions
public function addFollower(mc:MovieClip, frame:Function) {
mc.onEnterFrame = function() {
this.xy = {x:this._x, y:this._y};
this._parent.localToGlobal(this.xy);
this.fxy = {x:this.master.follow._x, y:this.master.follow._y};
this.master.follow._parent.localToGlobal(this.fxy);
if (this.xy.x < this.fxy.x && (!this.master.ob.hitTest(this.xy.x + this.master.speed + this._width, this.xy.y, true))) {
this._x += this.master.speed;
}
if (this.xy.x > this.fxy.x && (!this.master.ob.hitTest(this.xy.x - this.master.speed, this.xy.y, true))) {
this._x -= this.master.speed;
}
if (this.xy.y < this.fxy.y && (!this.master.ob.hitTest(this.xy.x, this.xy.y + this.master.speed + this._height, true))) {
this._y += this.master.speed;
}
if (this.xy.y > this.fxy.y && (!this.master.ob.hitTest(this.xy.x, this.xy.y - this.master.speed, true))) {
this._y -= this.master.speed;
}
this.frame();
this.master.frame();
}
mc.master = this;
mc.frame = frame;
}
}
HitTest Not Working
EDIT--> NEVER MIND, IT DOES WORK!
Does anyone know why this wouldn't work?
function initLogInMenu() {
attachMovie("LogInMenu","menu_LogIn", layer++);
menu_LogIn._x = 780;
menu_LogIn._y = 20;
menu_LogIn.onEnterFrame = function() {
if(this.hitTest(_root._xmouse, _root._ymouse)) {
gotoAndStop("Open");
trace("A");
}
else {
gotoAndStop("Close");
trace("B");
}
}
}
--------------------------
The MC is created and placed in the right spot, the onEnterFrame function is attached and working (I get a constant trace of "B") but when i mouse over the MC Nothing happens. i.e. My hit Test is not working.
Any ideas why not??
-Mike
Hittest Not Working
Hey..
I have a duplicating movieclip (acting as bullets) that shoot across the screen from a turret blah blah blah.. Their names are set as "bullet"+bulletcount; and everytime one is shot bullet++; and the result is many bullets called bullet1, bullet2, bullet3, bullet4 etc etc..
how do i get this to hit my wall i used the code
onEnterFrame = function() {
if ("bullet"+bulletcount.hitTest(_root.wall)) {
trace("hit")
}
}
However my code is not called as the bullets will pass straight over the wall and the code is called as soon as you fire the bullet....Why
Any help is appreciated..
HitTest Not Working?
Hi there.
It's probably my fault, but I can't get this simple hitTest to work. I had it working, but of course I tried to change something - and when I then had to set things back to normal it didn't work anymore.
I just can't see the (probably and hopefully) easy solution to this problem.
I have two movieclips. In movieclip "ball" a onEnterFrame(mouseDown) performs the hitTest. In movieclip "shoe", there is another mc "hit" - and this is the test parameters.
This is the code:
Code:
onClipEvent(mouseDown) {
if (this.hitTest(shoe.hit)) {
// do alot of stuff
}
}
Thanx in advance
How To Use HitTest Working With OOP
Hello People I am having a huge problem.
I want to use hitTest in a function defined in a class calle character. This class is linked with a movieclip called character too and I want to make a hitTest check with another movieclip called layer, so I do like this
ActionScript Code:
character.prototype.onEnterFrame = function(){
if (hitTest(layer)){
//do what it has to do ... but it returns true ALL THE TIME.... I've already made a trace in the variable layer. It returns _level0.layer what I guess is correct...
}
}
init = function(){
Object.registerClass("character", character);
character = _root.attachmovie("personagem", "personagem", 1);
layer = _root.attachmovie("layer", "layer", 2);
}
Could anyone help me... IF you do not understand the code but you know the answer please write the code as you 'd applied in your project that I will understand... The big question is ... how to use hitTest using OOP ??? because I dont want to user _root.layer.hitTest("_root.character") because if I do like this it is not OOP.... right?
Thanks by reading
Regards!
HitTest Not Working
Hi all, I'm trying to build a simple adventure game engine and I'm haveing problems getting the boundry hitTests from Ed Mack's tutorial to work. I'm pretty new to actionscript and if someone wouldn't mind taking a look at my code and telling me what I'm doing wrong here, I'd appreciate it. You can download the .fla Here. He's supposed to stay in the maze.Thanks.
HitTest Not Working
I have the following Movie and Frame scripts, can anyone see why my hit test is not working?
Movie Script:
onClipEvent(load) {
_root.initGame();
}
onClipEvent(enterFrame) {
_root.newPlane();
_root.movePlanes();
_root.movePlane();
// set the cursor
_root["cursor"]._x = _root._xmouse;
_root["cursor"]._y = _root._ymouse;
}
onClipEvent (mouseDown) {
// show blast
_root["cursor"].gotoAndPlay("blast");
// get the location of the click
x = _root._root["cursor"]._x;
y = _root._root["cursor"]._y;
// loop through all planes to see which was clicked
for (i=0; i<10; i++) {
// see if this was the one clicked
if (_root["plane"+i].hitTest(x, y, false)) {
// have the clip play the "hit" animation
gotoAndPlay(2);
// increase score and show it
score++;
}
}
}
Frame Script:
function initGame() {
// init plane variables
nextPlaneTime = 0;
nextPlane = 0;
planes = [];
numPlanes = 10;
}
function newPlane() {
// more planes?
if (nextPlane < numPlanes) {
// time for next?
if (getTimer() > nextPlaneTime) {
// create new plane clip
attachMovie("plane", "plane"+nextPlane, nextPlane);
_root["plane"+nextPlane]._x = -30;
// choose height of plane
_root["plane"+nextPlane]._y = int(Math.Random()*100)+20;
// add plane to array
planes.push({clip: "plane"+nextPlane, d: dx});
// set things up for next plane
nextPlane++;
nextPlaneTime = getTimer() + 2000;
}
}
}
function movePlanes() {
// loop through planes in array
for(i=planes.length-1;i>=0;i--) {
// get speed and clip
dx = planes[i].d;
plane = _root[planes[i].clip];
// move plane AT SPEED OF 10
plane._x += 10;
// plane exit right
} if ((dx > 0) and (plane._x > 570)) {
plane.removeMovieClip();
planes.splice(i,1);
}
}
stop();
Working With HitTest
I am trying to understand the hittest and could use some help. Here is the code i used:on (press) {
this.startDrag();
}
on (release) {
stopDrag();
}
//see if in center of target
onClipEvent (enterFrame) {
if (this._parent.target_mc.hitTest(this._x, this._y)) {
this._x = _parent.target_mc._x;
this._y = _parent.target_mc._y;
this._parent.target_mc._alpha = 50;
this.stopDrag();
}
}
It works fine but I am trying to figure out onhow to make just the front of the bulet works on the center of the target.Als how can I stop it from jumping into place like it does?
HitTest Not Working
i got this game im creating...
its a simple stickman that can move in all directions and shoot arrows based of the angle of his armes/bow. The point is hitting the enemy stickmen, but i cant seem to get the hitTest right, ive scanned alot on the forums, but cant find the error...
ActionScript Code:
</p>
<p>//delete if hit ground</p>
<p> trace("hittest")</p>
<p> if(this.hitTest(_root.ground_mc)) {</p>
<p> trace("hit")</p>
<p> arrowStopped = true</p>
<p> if(timerISset == false) {</p>
<p> oldTime = Math.round(getTimer() / 1000)</p>
<p> timerISset = true</p>
<p> }</p>
<p> if(oldTime - (Math.round((getTimer()/1000))-3) == 0) {</p>
<p> trace("del")</p>
<p> removeMovieClip(this)</p>
<p> }</p>
<p> }</p>
<p> //delete if hit ground end</p>
<p>
the target is right too, and is just a box, moving after the players legs.
the file is too large to be added, even tho i zipped it...
HitTest Not Working
I have tried hitTest many times. The function works perfect if I run it from an example, but not if I do it myself.
Could someone please look at my file. I attached the FLA.
Also, in this demonstration I tried using the if with the ,s. I have tried with _root.mc.hitTest style.
Please see if you can find my error.
This is for CS3.
Thanks,
Matt
HitTest Not Working...
Hi--
I am using the following script to set up a hitTest. The basic hitTest works fine, but the second part of the if statement (&& (_root.w2._currentframe==1||2)--see below) doesn't have any effect. I am trying to set limitations on the hitTest so that "this.responsew1" only moves to the next frame if both conditions of the if statement are met.
I must be doing something wrong, but lord help me, I don't see what!
Can anyone help? It would be greatly appreciated!!!
onClipEvent (enterFrame) {
if (this.hitTest(_root.w2) && (_root.w2._currentframe==1||2)) {
with (this.responsew1) {
if (_currentframe == 1) {
gotoAndPlay(2); }
}
}
}
HitTest Not Working
I have the following Movie and Frame scripts, can anyone see why my hit test is not working?
Movie Script:
onClipEvent(load) {
_root.initGame();
}
onClipEvent(enterFrame) {
_root.newPlane();
_root.movePlanes();
_root.movePlane();
// set the cursor
_root["cursor"]._x = _root._xmouse;
_root["cursor"]._y = _root._ymouse;
}
onClipEvent (mouseDown) {
// show blast
_root["cursor"].gotoAndPlay("blast");
// get the location of the click
x = _root._root["cursor"]._x;
y = _root._root["cursor"]._y;
// loop through all planes to see which was clicked
for (i=0; i<10; i++) {
// see if this was the one clicked
if (_root["plane"+i].hitTest(x, y, false)) {
// have the clip play the "hit" animation
gotoAndPlay(2);
// increase score and show it
score++;
}
}
}
Frame Script:
function initGame() {
// init plane variables
nextPlaneTime = 0;
nextPlane = 0;
planes = [];
numPlanes = 10;
}
function newPlane() {
// more planes?
if (nextPlane < numPlanes) {
// time for next?
if (getTimer() > nextPlaneTime) {
// create new plane clip
attachMovie("plane", "plane"+nextPlane, nextPlane);
_root["plane"+nextPlane]._x = -30;
// choose height of plane
_root["plane"+nextPlane]._y = int(Math.Random()*100)+20;
// add plane to array
planes.push({clip: "plane"+nextPlane, d: dx});
// set things up for next plane
nextPlane++;
nextPlaneTime = getTimer() + 2000;
}
}
}
function movePlanes() {
// loop through planes in array
for(i=planes.length-1;i>=0;i--) {
// get speed and clip
dx = planes[i].d;
plane = _root[planes[i].clip];
// move plane AT SPEED OF 10
plane._x += 10;
// plane exit right
} if ((dx > 0) and (plane._x > 570)) {
plane.removeMovieClip();
planes.splice(i,1);
}
}
stop();
How To Use HitTest Working With OOP
Hello People I am having a huge problem.
I want to use hitTest in a function defined in a class calle character. This class is linked with a movieclip called character too and I want to make a hitTest check with another movieclip called layer, so I do like this
ActionScript:
character.prototype.onEnterFrame = function(){
if (this.hitTest(layer)){
//do what it has to do ... but it returns true ALL THE TIME.... I've already made a trace in the variable layer. It returns _level0.layer what I guess is correct...
}
}
init = function(){
Object.registerClass("character", character);
character = _root.attachmovie("personagem", "personagem", 1);
layer = _root.attachmovie("layer", "layer", 2);
}
Could anyone help me... IF you do not understand the code but you know the answer please write the code as you 'd applied in your project that I will understand... The big question is ... how to use hitTest using OOP ??? because I dont want to user _root.layer.hitTest("_root.character") because if I do like this it is not OOP.... right?
Thanks by reading
Regards!
Why Is This HitTest Not Working?
Hey all.
Here is the code i have:
Actionscript Code:
turret_array = new Array();_global.create_normal_turret = function() { turret_num = turret_array.length; attachMovie("turret", "turret_"+ turret_num, getNextHighestDepth()); turret = "turret_" + turret_num; turret._x = _xmouse; turret._y = _ymouse; startDrag(turret, true); playing_area.onPress = function() { if(turret_num == 0){ turret_array.push(turret); stopDrag(); }else{ for(i=0; i<turret_num; i++){ tname = "turret_" + i; if(turret.hitTest(tname)){ _root.health = 50; } } } }
}
when i place the first turret it is called "turret_0"
when i call the function for the 2nd time it creates a turret called "turret_1"
When it runs through the for loop, and i trace(turret) and trace(tname), it shows turret_1 and turret_0, correct so far?
but when it has to do the hitTest, it never seems to register that it has hit(_root.health = 50 does not show up in my dynamic text box).
Any idea's why?
Thanks,
Gareth
HitTest Not Working
Hello
I'm having a bit of trouble with my hitTest code. I cant figure out why trace wont fire?
I have a mc (nineeightfour_mc) that hitTests with nineeightfourAccept_mc
My Code:
Code:
_root.content_mc.content1_mc.nineeightfour_mc.onRelease = function(){
stopDrag()
if(this.hitTest(nineeightfourAccept_mc)){
trace("hit")
}else{
setProperty(this, _x, 191.3)
setProperty(this, _y, 76)
}
}
Any ideas? I know its something simple right?
Thanks for any help
Greg
HitTest Not Working :(
Hello
This compiles but does not work
ActionScript Code:
if(qs.hitTest(_parent.bc)) {
trace("hit baby");
}
So I tried
ActionScript Code:
function pcollision() {
if(qs.hitTest(_parent.bc)) {
trace("hit baby");
}
}
then on my button to provoke this function
ActionScript Code:
q1_down.onPress = function() {
_parent.qs.startDrag();
_parent.qs._alpha=50;
_parent.pcollision();
}
This didn't work either
Sandman9
HitTest Not Quite Working
Hello guys.
~Well i just about got this game finished and guess what yes some thing has gone wrong. I was re-coding parts of the game so that i could incorperate it into my main site. It should have been just a case of re directing variable paths but some thing has gonew wrong some where and i dont kno what, i think it might be the getBounds script for the hitTest.
ActionScript Code:
b = this.getBounds(_root);
//trace("b maXx="+b.xMax+" b minX="+b.xMin+" b miny="+b.yMin+" b Maxy="+b.yMax);
this.onEnterFrame = function() {
if (_parent.ball._x<b.xMax and _parent.ball._x>b.xMin and _parent.ball._y>b.yMin and _parent.ball._y<b.yMax) {
_parent.score += 5;
_parent.ball.yspeed *= -1;
gotoAndStop(10);
}
};
The _root of the movie holds the mc with the game in it so i thought maybe this:
b = this.getBounds(_root);
Should be this
b = this.getBounds(_root.game);
But nothing. The mc that holds the game has the instance name game.
game link
Anyone
Cheers
_paul
HitTest Not Working When MC Is Loaded Into A New MC
Hi
I have here 2 Movies:
1. "ex_maz_test.swf": is a simple maze based on Macromedia FMX samples, when I run the movie the whole thing works.
2. "loader_test.swf": a movie that loads the first ex_maz_test MC, when I run this movie the maze doesn't work the way you would expect it to work. it seems like the movie gets different maze coordinates and ignores the maze you see on the stage.
I've ran all kind of tests but could not find the reason, this is the first time I use hitTest and I was wondering if there's a path problem here.
below you can see the script I have in "ex_maz_test.swf, I am attaching the 2 movies and you can run the maze separately and from the loader movie and see the bugs.
any help would be appreciated
Thanks
LeoBeer
------------------------------------
ex_maz_test
Code:
onEnterFrame = function () {
with (square) {
//
// keyboard controls
if (Key.isDown(Key.DOWN)) {
_y += 1;
}
if (Key.isDown(Key.UP)) {
_y -= 1;
}
if (Key.isDown(Key.LEFT)) {
_x -= 1;
}
if (Key.isDown(Key.RIGHT)) {
_x += 1;
}
//
// detect if edges of the player square are colliding with the maze _root.exer.maze.walls
if (maze.walls.hitTest(getBounds(_root).xMax, _y, true)) {
_x -= 1;
}
if (maze.walls.hitTest(getBounds(_root).xMin, _y, true)) {
_x += 1;
}
if (maze.walls.hitTest(_x, getBounds(_root).yMax, true)) {
_y -= 1;
}
if (maze.walls.hitTest(_x, getBounds(_root).yMin, true)) {
_y += 1;
}
}
};
stop();
GotoAndPlay Not Working With HitTest
Code:
onClipEvent (enterFrame) {
if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
_root.thingy.gotoAndPlay(9);
_root.dawg = "hitted.";
} else {
_root.thingy.gotoAndStop(1);
_root.dawg = "unhitted.";
}
}
won't play. it got to that frame and stops. even a play() in the 9th keyframe of the mc wont do it.
HitTest Not Working Heeelllllllllllppppppp
Hi
I am trying to make an mc scale up and down depending on if the mouse is over the mc area or not. i have the following code and it is also attahced(.fla):
_root.test.onEnterFrame = function() {;
if(this.hitTest(this._xmouse,this._ymouse)){
scale(this, 200, 200, 1);
} else {
scale(this, 100, 100, 1);
}
}
// scale bounce function //////////////////////////////////////////////////////////////////
function scale(target, xscale, yscale, speed) {
target.yscale = yscale;
target.xscale = xscale;
target.speed = speed;
target.viscocity = 1.5;
target.vy = 0;
target.vx = 0;
target.onEnterFrame = function() {
var dx = this.xscale-this._xscale;
var dy = this.yscale-this._yscale;
this.vy = (this.vy+dy/this.speed)/this.viscocity;
this.vx = (this.vx+dx/this.speed)/this.viscocity;
this._yscale += this.vy;
this._xscale += this.vx;
if (Math.round(dx) == 0 && Math.round(vx) == 0 && Math.round(dy) == 0 && Math.round(vy) == 0) {
delete this.onEnterFrame;
}
};
}
But when I move my mouse over the mc it doesnt do anything...it should scale to 200% when over and scale down to 100% when out of the area. Can anyone see where im going wrong.
Thanks in advance.
HitTest Loop Not Working Right.....
Hi all,
i am trying to make a tetris-like game, and am having problems with the pieces stopping when they hit other pieces in 'the box'. All pieces are duplicated onto the scene, and there names recorded in an array (placedp[]). Now, as each new piece comes onto the scene, it should keep checking that it has not hit another piece( or the bottom of the box ) whilst it is moving. i do this with the following piece of code:
Code:
for(i=0;i<_root.placedp.length;i++){
if(this.hitTest(_root[placedp[i]])){
this.hit=true;
i=38;
}else{_root.gametext2=_root.placedp[i];}
}//end for loop
if it hits the bottom, or another piece, a variable(var hit) held within 'the focus piece' will be set, making it stop, and not let the user run any more movement commands on it. This works fine when i replace "(_root[placedp[i]]" with a name of a piece on the board(when i make it hit that piece anyway), i have checked outputs of placedp array and length etc, and it should definitely work.
----------------------------------------------------
this section "else{_root.gametext2=_root.placedp[i];}" is just part of my value checking, writing the values to screen for me to see. I would have thought that this value would keep changing, as the placedp array gets more and more names in it, but it stays static.......maybe looping to fast to make the changes.....pieces do stop when suppossed to, when i substitute what i said above......................
thanks all
MW
Simple HitTest Not Working...
hi.. here is the code:
see anything wrong? i simply don't get any trace outputs...
PHP Code:
onClipEvent (load) {
this.speed = 0;
}
onClipEvent (enterFrame) {
if (this.hitTest(_root.sol._x, _root.sol._y, true)) {
trace("true");
} else {
if (this.speed<7) {
this.speed += .3;
}
this._y += this.speed;
}
}
Hittest On Shape Nearly Working...
Hi All,
how you all doing? I have a question that I hope that someone on here will be able to answer for me.
I have been trying to set up a hittest between three mc's in my .fla - one mc is called cursor - this mc follows the mouse position around the stage - until the cursor mc comes into contact with either of the other two mc's (obstacle1 & obstacle2) - when the cursor mc touches the obstacle is stays at the border/bounding box of the obstacle.
I need the cursor to recognise and follow the shape of the obstacle and not the bounding box.
So far I have managed to get the cursor to recognise the actual shape of the obstacle mc - but now the problem is when it touches the obstacle shape it then jumps back to the obstacles bounding box! :-(
I have been plugging away at this for some time but am not able to make the cursor recognise and follow the obstacles shape - the cursor mc will follow the mouse to the shape but then jumps back to the bounding box!
This is the script from the cursormanager.as file:
PHP Code:
class CursorManager
{
private var obstacleList:Array;
private var cursor:MovieClip;
public function CursorManager(cursorClip:MovieClip)
{
cursor = cursorClip;
obstacleList = new Array();
}
public function addObstacle(obstacle:MovieClip):Void
{
obstacleList.push(obstacle);
}
public function updateCursorPosition():Void
{
var mouseX:Number = _level0._xmouse;
var mouseY:Number = _level0._ymouse;
// Default to mouse position.
cursor._x = mouseX;
cursor._y = mouseY;
// Move the cursor if it is inside an obstacle.
for (var i:Number = 0; i < obstacleList.length; i++)
{
// Assumes that the obstacles are children of _level0.
// Might need to use the Rectagle classes intersection function to
// determine if the cursor is intersecting.
if (obstacleList[i].hitTest(_root.cursor._x,_root.cursor._y,true))
{
moveCursorOutsideObstacle(obstacleList[i], _root.cursor._x,_root.cursor._y);
break;
}
}
}
public function moveCursorOutsideObstacle(obstacle:MovieClip, mouseX:Number, mouseY:Number):Void
{
var xPos:Number = mouseX;
var yPos:Number = mouseY;
// Move towards the closest axis.
var x1:Number = xPos - obstacle._x;
var x2:Number = obstacle._x + obstacle._width - xPos;
var y1:Number = yPos - obstacle._y;
var y2:Number = obstacle._y + obstacle._height - yPos;
var closestX:Number = Math.min(x1, x2);
var closestY:Number = Math.min(y1, y2);
if (closestX < closestY)
{
if (x1 < x2)
{
xPos = obstacle._x - cursor._width;
}
else
{
xPos = obstacle._x + obstacle._width;
}
}
else
{
if (y1 < y2)
{
yPos = obstacle._y - cursor._width;
}
else
{
yPos = obstacle._y + obstacle._height;
}
}
cursor._x = xPos;
cursor._y = yPos;
}
}
What do I change to get the cursor to move around the shape of the obstacle and not just it's bounding box???
Anyone?
HitTest Shapeflag Not Working
Hi, it's been a long while since I posted here. I've gotten the shapeflag for hitTest to work before on some of my projects but this latest one is confusing me.
I currently have the following structure:
_root.bgstg
where bgstg is a mc
bgstg contains a character and some obstacles and a side scrolling land.
aim: make the character detect the land if it touches the land / obstacle and "die"
i have onEnterFrame for bgstg.land.detectMe.hitTest(player._x, player._y, true)
the shapeflag just isn't working.
Would really appreciate any advice for this.
Thanks in advance.
HitTest Not Working Anymore
Hello, i'm having trouble using hitTest in a videogame.
I have 2 movieclips on "root". One is a movieclip containing my character and the other one is a movieclip containing a freeform to check collision with my character.
Everything is working ok, and collision works great. The problem arises when i try to embed ALL my game into another flash movie (to use as a game selector, and embed there several movieclips loading them). Everything works BUT the collision
Here is th code i'm using:
Code:
if (motion_path.hitTest(JohnnyfeetX, JohnnyfeetY, true))
{
Johnny._y-=0.2;
}
JohnnyfeetX and JohnnyfeetY are x and y coords for the bottom of my character, calculated some lines before.
Anybody knows what happens?
HitTest For Game Not Working
Here it is in action:
http://www.philosophyzero.com/hittest.swf
See how it screws up on the part that sticks out?
This is the code:
Code:
onEnterFrame = function(){
_root.hit();
_root.move();
}
function hit() {
with(_root.box) {
if (_root.wall.hitTest(getBounds(_root).xMax, _y, true)) {
_root.wall._x += 1;
}
if (_root.wall.hitTest(getBounds(_root).xMin, _y, true)) {
_root.wall._x -= 1;
}
if (_root.wall.hitTest(_x, getBounds(_root).yMax, true)) {
_root.wall._y += 1;
}
if (_root.wall.hitTest(_x, getBounds(_root).yMin, true)) {
_root.wall._y -= 1;
}
}
}
//scrolling
function move() {
if (Key.isDown(Key.DOWN)) {
wall._y -= 1;
}
else if (Key.isDown(Key.UP)) {
wall._y += 1;
}
else if (Key.isDown(Key.LEFT)) {
wall._x += 1;
}
else if (Key.isDown(Key.RIGHT)) {
wall._x -= 1;
}
}
Real simple, huh? But I can't for the life of me figure out how to perfect it.
HitTest Not Working Correctly
I'm trying to get my character to rotate when he goes up and down slopes, but for some reason it isn't hit testing correctly. I've tried different methods of rotating the character - using the standard movieclip.rotation and also the matrixtransformer and rotateAroundExternalPoint to rotate the character, but it simply isn't recognising when the character is hitting the floor, which I can't make sense of!
The hit test is performed in the upper left of the screen in a 3x3 tile bitmap which is called hitTestBitmap. I'm hit testing the duplicateCharBitmap object against this but it simply isn't working correctly.
If anyone has the time could you look at my code (sorry its rather messy at the moment) and see what's going wrong with it?
Thanks a lot for ANY help as I am completely stuck!
Code download: Link
Movie Loaded With Hittest Not Working
I have completed a maze game which works quite fine. But when i m loading that movie in my interface, it stops working.
I know that relative paths must be i use. I used realtive paths everywhere.But i think i know where problem is but doesn't know how to rectify that.
At one place i hva used this code
///** Here is my code ***/////
if(this._parent.lan.path.hitTest(_x-x,_y-y,true))
{
this._parent.land._x+=x;
this._parent.land._y+=y;
}
///** END ***/////
I will be very thankful if some one can figure out where i m wrong.
Thanks.
Creative Saby
Movieclip & Array With HitTest, Not Working
Ok, my problem is that I have a grassfield named: "grass1". I put it an array named:"grass" with the index of 0. I want to use the array in a hitTest to make the guy stop. I know how to do the stop functions, but I don't think I am doing the array right. Here is all of my code so far:
Code:
movespeed = 10;
bg = new Array(_root.bg0);
objects = new Array(_root.bg0.grass0, _root.bg0.grass1);//My grass array
_root.guy.onEnterFrame = function() {
if (Key.isDown(37)) {
this._x -= movespeed;
}
if (Key.isDown(39)) {
this._x += movespeed;
}
if (Key.isDown(38)) {
this._y -= movespeed;
}
if (Key.isDown(40)) {
this._y += movespeed;
}
if (grass[0].hitTest(this)) {//This is the hitTest I am having troubles with
this._x -= movespeed;
}
};
Now, how can I get the hitTest to work properly with the array?
HitTest Isnt Working For Some Reason
hi there,
im trying to get two movie clips to collide, then goto frame 1 in the animation. Ive searched thru google and it looks like i might have named my instances wrong or something because the code looks correct. [FlashMX]
objects: 'chutters' - instance of: chutters (this is a movieclip i have 10 of) these are asteroids
objects: 'laura' - instance of: laura (this is the players character)
Code:
//In the laura movieclip actionscript
onClipEvent (enterFrame) {
if (this.hitTest (_root.chutters)) {
gotoAndPlay(2);
}
}
Hittest Suddenly Stop Working
It just suddenly stops: every script with "hitTest" stops at what seems like random times. Is there a reason that sothing would affect all hitest?
I have like 4 part wich have hitest and they all stop at the same time.
HitTest Not Working Properly With StartDrag
I am having trouble getting hit test to behave properly. I am using StartDrag to move an mc across the screen. When it rolls over another mc, an animation begins. I have tried a HitTest between the 2 objects as well as with one object and the cursor. In both cases the I can move across the target area slowly and nothing happens but when I zip across the screen; it works. Is there a way to increase the frequency of the HitTest? I tried increasing the frame rate but that actuallly made it worse.
Sample code:
function iconDrag2() {
_root[icons[4]].startDrag(true);
if (_root[icons[4]].hitTest(_root[whitepieces[4]])) {
_root[whitepieces[4]].gotoAndPlay(2);
trace("it's a hit");
};
}
[f8] Hittest Stops Working After Loadclip
Dear Readers,
I have encountered a very irritating problem with the game i'm developing, below I will describe what my problem is. (In My best English )
Scenario:
1 main flash movie called game.swf
1 external flash movie called maze.swf
The thing is from my main movie game.swf I load the external "maze.swf" true the LoadClip procedure. This works "fine".
The maze.swf as you can guess contains a maze game, and as you all will know this works with the Hittest commando to check if the character doesn't walk up against the wall.
If I start the maze.swf as a stand-a-lone this works fine, but as soon as I load maze.swf in my main movie. The hittest doesn't work anymore, because in some way the hittest is now checking with the coordinates of the entire main movie instead of only the boundaries of the maze.
I checked If I was calling to the right path, with _root and _level0 and etc. This is al correct because I can trace all the properties from maze.swf in my main movie. But the hittest simply won't work.
Does anyone has a tip or hint so i can check that out.
Thanks in advance.
B_phoenix
Hittest Not Working Off Stage - Sollution?
Hi there
I'm encountering a problem when I use a hittest to check if the mouse cursor is within a movieclip. The detection works fine when the mouse pointer stays within the boundries of the stage, but not if the mouse moves out of the stage.
PHP Code:
_root.onEnterFrame = function(){
if(_root.mc_rectangle.hitTest(_xmouse, _ymouse, true)){
_root.txt_status.text = "INSIDE";
} else {
_root.txt_status.text = "OUTSIDE";
}
}
So for instance, when the movieclip is aligned with one of the sides of boundries of the stage the hittest will never return false when you move the mouse outside the movieclip through that side. At the moment you leave the stage, the hittest stops recording and keeps returning true (as though it's still inside the movieclip).
A potential sollution that does NOT work: making a border between the mc and the stage so the mouse has to pass over an empty part of the stage.
This doesn't always work and certainly not if you move your mouse too fast.
i've found another 'sollution' on this forum that includes a velocity or something, but that seems a bit weird and unreliable to me.
Does anyone have a suggestion on how to catch when the mousepointer is outside of the stage?
HitTest Not Working With Internal Shape
I want to be able to detect a collision with the internal shape of a movie clip. I found in tutorials to use the following code, but it won't work....help?
Code:
onEnterFrame = function(){
if(blackBall.hitTest(blueBall,true)){
trace("hit");
}
}
HitTest Funcion Not Working Properly..
Hi all,
I have a problem with flash hitTest() function.
It is not working properly.I have attached two pdf files with this post for reference. I have mentioned each & Every thing in this pdf attachment.
So if anyone have any idea about this problem then pls help.
Thanks In Advance.....
|