hitTest help!

probably something simple

Page 1 of 1

9 Replies - 1992 Views - Last Post: 04 November 2009 - 04:24 PM

#1 Tom_Collier  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 17-September 09

hitTest help!

Post icon  Posted 02 November 2009 - 04:25 PM

Evening all!

i've taken the plunge and i'm learning actionscript 2.0!
but i find it hard to follow all the books and resources available to me except direct help, because i'm new to OOP languages.

So onto the main thang,
i'm just going through things making random stuff but using features and learning the syntax, and todays task was hitTest.
but its not working :/

wondered if you guys could take a look

*DO NOT GIVE ME CORRECTED CODE*
but point me to my error, even explain exactly what it is, but let me figure out what to do by myself, or i won't learn

whew, thats a keyboardfull, thanks guys!

Its not letting me upload my .fla file, so here's the layout

2 movie clips, instance names fgzombmc and ballmc

Code In The Frame :
onEnterFrame = function() {
	if (Key.isDown(39)) {
	fgzombmc._x += 3
	}
	if (Key.isDown(37)) {
		fgzombmc._x -= 3
	}
	if (Key.isDown(38)) {
		fgzombmc._y -= 3
	}
	if (Key.isDown(40)) {
		fgzombmc._y += 3
	}
}



Code In ballmc :
onClipEvent(load)
{
	xcorrect = (_root.fgzombmc._width/2);
	ycorrect=(_root.fgzombmc._height/2);
}
onClipEvent(enterFrame)
{
	if (this.hitTest(_root.fgzombmc._x - xcorrect,_root.fgzombmc._y - ycorrect,true)==true){
	this.color = Red
	}
		}



you experts will grasp this, but my aim is to make the ball go red.

thanks again!

Is This A Good Question/Topic? 0
  • +

Replies To: hitTest help!

#2 aedesign53  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-October 09

Re: hitTest help!

Posted 02 November 2009 - 07:46 PM

Actually I am not sure what you are trying to do. I seems for the first code for fgzombmc you are trying to make it move on the stage and you want to use the down key to do so. The second code for ballmc you are trying to use the x and y of fgzombmc to change the color of ballmc. Is this correct?
Here hint. If you have entered the code on the movieclips them self you will not be able to get the ballmc code to see then fgzombmc x and y corrodiantes.
you might want to try creating an actionscript layer on the main time line and place all you code there so the code you have for each object can see each other

View PostTom_Collier, on 2 Nov, 2009 - 03:25 PM, said:

Evening all!

i've taken the plunge and i'm learning actionscript 2.0!
but i find it hard to follow all the books and resources available to me except direct help, because i'm new to OOP languages.

So onto the main thang,
i'm just going through things making random stuff but using features and learning the syntax, and todays task was hitTest.
but its not working :/

wondered if you guys could take a look

*DO NOT GIVE ME CORRECTED CODE*
but point me to my error, even explain exactly what it is, but let me figure out what to do by myself, or i won't learn

whew, thats a keyboardfull, thanks guys!

Its not letting me upload my .fla file, so here's the layout

2 movie clips, instance names fgzombmc and ballmc

Code In The Frame :
onEnterFrame = function() {
	if (Key.isDown(39)) {
	fgzombmc._x += 3
	}
	if (Key.isDown(37)) {
		fgzombmc._x -= 3
	}
	if (Key.isDown(38)) {
		fgzombmc._y -= 3
	}
	if (Key.isDown(40)) {
		fgzombmc._y += 3
	}
}



Code In ballmc :
onClipEvent(load)
{
	xcorrect = (_root.fgzombmc._width/2);
	ycorrect=(_root.fgzombmc._height/2);
}
onClipEvent(enterFrame)
{
	if (this.hitTest(_root.fgzombmc._x - xcorrect,_root.fgzombmc._y - ycorrect,true)==true){
	this.color = Red
	}
		}



you experts will grasp this, but my aim is to make the ball go red.

thanks again!

Was This Post Helpful? 0
  • +
  • -

#3 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: hitTest help!

Posted 03 November 2009 - 05:01 AM

Your problems are twofold, you're checking to see if the ball hits certain coordinates which it never does, and the color change code is wrong.

Lets forget about the color change for the moment, and just make sure the ball's hitTest is ever evaluating true. To do this, put a trace in the if...hitTest codeblock, so that you will know for sure when your hitTest is returning true. With this trace in place, you will see that the hitTest is not true when fgzombmc is over ballmc. That is because you're subtracting the xcorrect and ycorrect values rather than adding them here:
if (this.hitTest(_root.fgzombmc._x - xcorrect,_root.fgzombmc._y - ycorrect,true)==true){

Also, you don't need to have the ==true on this line. HitTest returns a true or false value, which is all an if needs.

For the color change, you can't just use color = red because MovieClips do not have a color property. Instead you have to create a color object with ballmc as the target, and use the Color object's setRGB function to set a new color. Normally at this point I would post the code for this, but since you asked I'll refrain :)

One last thing: if you want to upload your fla you'll need to put it in a zip file first.
Was This Post Helpful? 0
  • +
  • -

#4 Tom_Collier  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 17-September 09

Re: hitTest help!

Posted 03 November 2009 - 03:15 PM

aedesign53 : What i'm trying to do is to make my movable zombie character hit a ball and make it go red when it hits it.

So Your saying,
1) put all my code into an actions layer instead of in seperate places

thehat : You certainly know your stuff!
So if i try to break down exactly what your saying into a few key points, your advice would be,

1) Use traces to check where my error is, my output or my function,
2) Add my xcorrect and ycorrect instead of subtracting them,
3) I don't need to ask whether the itest is true, thats preprogrammed.
4) I should use RGB ( That if i remember 255,255,255 and such) to change my colours?

This post has been edited by Tom_Collier: 03 November 2009 - 03:16 PM

Was This Post Helpful? 0
  • +
  • -

#5 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: hitTest help!

Posted 04 November 2009 - 06:30 AM

Add the xcorrent and the ycorrent in your if, yes. That's the only change I had to make to get the hit detection to work. For the color change, you have to do something like this:
myColor:Color = new Color(myTargetObject);
myColor.setRGB(0xFF0000);


The RGB value is given in hex, this makes the target object red.
Was This Post Helpful? 1
  • +
  • -

#6 Tom_Collier  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 17-September 09

Re: hitTest help!

Posted 04 November 2009 - 10:11 AM

Ok thanks guys!
i'll set to work changing it, and i'll learn a bit of hex terminology :D
time for ver.2
Was This Post Helpful? 0
  • +
  • -

#7 Tom_Collier  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 17-September 09

Re: hitTest help!

Posted 04 November 2009 - 12:52 PM

Right Gang!
i've finished version 2, and a couple more problems have arisen, because i've added some new features based on logical thinking alone :D

1) i have a working hitTest and colour change, but its not a repeatable colour change, it only changes once. I've commented out the code i tried to use instead, but i had errors.

2) i've tried to make my movieclip change position after the hitTest, i've commented out my attempt for that.

i'll upload the flash file now for ease of access.

once again, thanks a bunch!

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#8 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: hitTest help!

Posted 04 November 2009 - 01:54 PM

So you will have noticed that the color change affects the whole of ballmc. If you only wanted the inner circle to change color you will have to make that a movieclip and target it with your color change:
new Color(ballmc.innerCircle).setRGB(0xFF0000);


To make the change repeatable you just have to have an else clause to your if that sets the color back again. To make this easier and a bit cleaner I suggest you use the Delegate class to handle your enterFrame event. The benefit of this is that the function that is called each frame will retain the scope of the root, rather than the enterFame event object. It also means that you can define the color object outside of the enterFrame function. So I would replace your first line with this:
import mx.utils.Delegate;
onEnterFrame = Delegate.create(this, enterFrameHandler);

var colorChange:Color = new Color(ballmc.inner);

function enterFrameHandler(){


This means that your color change code now only needs to look like this:
colorChange.setRGB(0xFF0000);

And you can easily use the same line with the yellow hex code(FFF00) to change it back again in an else clause of your hitTest if.

For your second problem, Math.random is a function, and so must be used with brackets: Math.random()
Was This Post Helpful? 1
  • +
  • -

#9 Tom_Collier  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 17-September 09

Re: hitTest help!

Posted 04 November 2009 - 02:53 PM

happy with everything but the delegate.

Quote

To make this easier and a bit cleaner I suggest you use the Delegate class to handle your enterFrame event. The benefit of this is that the function that is called each frame will retain the scope of the root, rather than the enterFame event object. It also means that you can define the color object outside of the enterFrame function.


just so i'm clear, in laymans terms, the delegate function lets me keep my colour variable seperate from the enter frame so that i can use it on every frame i want. AND it lets my code look and run simpler yes?

i feel like a right helpless git :D
is it like this for everyone?
Was This Post Helpful? 0
  • +
  • -

#10 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: hitTest help!

Posted 04 November 2009 - 04:24 PM

When you do onEnterFrame = function() {, the scope of the execution can be transferred to the object that has experienced the event. This isn't so significant with your onEnterFrame because the object in question is the root, and so the scope stays put. If you were targeting the enterFame of a different movieclip however, the scope would shift to the movieclip who had entered a new frame, and so calling variables or functions that are on the root would no longer work because they wouldn't exist in your movieclip. So while your code would still work without the Delegate in this instance, it's better practice to use it. Also, it's more in keeping with the methodology of actionscript 3, which is a much more powerful version of actionscript. In AS3, you would set up an enterFrame listener like this:
this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
function enterFrameHandler(evt:Event):void {
   //do something every frame
}


As you can see this isn't so far removed from my suggestion of using Delegate.

Also, the benefit for you comes from you not declaring a new Color object every frame, but instead using the one you initialize before the event handler is called for the first time. And yes, I think it is like this for everyone to start with. I've been working with Flash for years and cliched as it may sound I'm still learning.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1