Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Sprite.hitArea - property of Actionscript 3.0

This is a discussion on Sprite.hitArea - property of Actionscript 3.0 within the Tutorials forums, part of the Flash English category; I remember when using Flash 8 I needed once in a while to create a 'transparent' area such as an ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 25-09-07, 16:43
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Sprite.hitArea - property of Actionscript 3.0

I remember when using Flash 8 I needed once in a while to create a 'transparent' area such as an Hotspot onto which I would apply action on determinate mouse events.

As an example, I was creating a MovieClip of the desired size and transparent colour.* Using the 'old' hitTest, I was assigning actions as the mouse was rolling over that area.*
Surely, this method would still be valid using the new method hitTestObject as seen in the article hitTest with Flash CS3 .  Now though Actionscript 3.0 has a new property of the Sprite Class called hitArea (usable also with the MovieClip class as it inherits from the Sprite Class) available.
*Let's see how to use it' As always, I create a FLA and save it as 'main.fla' of the dimension 550x300, into which I insert:
- a MovieClip (registration point top centred) consisting of a rectangle of the size 550x100 and of transparent colour. I name it 'hit_area_mc' and position it at y=10
- a second MovieClip (registration point bottom centred) containing menu items and a rectangle of the size 550x100. I name it 'menu_mc' and position it to y=10.

2 MovieClip

I create a Document Class, an AS file saved as 'Main.as', implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	
	public class Main extends MovieClip
	{
		private var arriveY:int;
		
		public function Main()
		{
			init();
		}
		
		private function init():void
		{
			stage.frameRate=31;
			
			menu_mc.hitArea=hit_area_mc;
			hit_area_mc.mouseEnabled=false;

			menu_mc.addEventListener(MouseEvent.MOUSE_OVER,setOver);
			menu_mc.addEventListener(MouseEvent.MOUSE_OUT,setOut);
		}
		
		private function setOver(m:MouseEvent):void
		{
			arriveY=98;
			m.currentTarget.removeEventListener(Event.ENTER_FRAME,closeMenu);
			m.currentTarget.addEventListener(Event.ENTER_FRAME,openMenu);
		}
		private function setOut(m:MouseEvent):void
		{
			arriveY=10;
			m.currentTarget.removeEventListener(Event.ENTER_FRAME,openMenu);
			m.currentTarget.addEventListener(Event.ENTER_FRAME,closeMenu);
		}
		
		private function openMenu(e:Event):void
		{
			var dy:Number=arriveY-e.currentTarget.y;
			var ay:Number=dy*.2;
			e.currentTarget.y+=ay;
			if(Math.abs(dy)<=.2)
				e.currentTarget.removeEventListener(Event.ENTER_FRAME,openMenu);
		}
		private function closeMenu(e:Event):void
		{
			var dy:Number=arriveY-e.currentTarget.y;
			var ay:Number=dy*.2;
			e.currentTarget.y+=ay;
			if(Math.abs(dy)<=.2)
				e.currentTarget.removeEventListener(Event.ENTER_FRAME,closeMenu);
		}
	}
}
Rolling over the menu, the result is the following:





Let's analyse the code:

Properties

A numerical variable (int) to which I assign an arrival Y point to apply the inertia effect to 'menu_mc'
private var arriveY:int;

Methods
init();
I impost the frame rate
stage.frameRate=31;
now we will see how to declare the property hitArea.
we assign the MovieClip 'hit_area_mc' to the property hitArea of 'menu_mc'
menu_mc.hitArea=hit_area_mc;
to have it function properly, we need to impost the property mouseEnabled of the MovieClip 'hit_area_mc' to false
hit_area_mc.mouseEnabled=false;
I add the two listeners to the MOUSE_OVER and MOUSE_OUT on the 'menu_mc' which will call the respective methods setOver and setOut
menu_mc.addEventListener(MouseEvent.MOUSE_OVER,set Over);
menu_mc.addEventListener(MouseEvent.MOUSE_OUT,setO ut);

setOver();
I impost the variable 'arriveY' to 98 ('menu_mc' will move with the inertia effect to the coordinate y 98 and open the menu)
arriveY=98;
I remove the listener ENTER_FRAME which calls the method closeMenu (explained next)
m.currentTarget.removeEventListener(Event.ENTER_FR AME,closeMenu);
I add a listener ENTER_FRAME which call the method openMenu (explained next)
m.currentTarget.addEventListener(Event.ENTER_FRAME ,openMenu);

setOut();
I impost the variable 'arriveY' to 10 ('menu:mc' will move with the inertia effect to the coordinate y 10 and close the menu)
arriveY=10;
I remove the listener ENTER_FRAME which calls the method openMenu (explained next)
m.currentTarget.removeEventListener(Event.ENTER_FR AME,openMenu);
I add a listener ENTER_FRAME which calls the method closeMenu (explained next)
m.currentTarget.addEventListener(Event.ENTER_FRAME ,closeMenu);

openMenu();
I apply the inertia effect to the y axe of 'menu_mc' based on the value of the variable 'arriveY' (the menu opens)
var dy:Number=arriveY-e.currentTarget.y;
var ay:Number=dy*.2;
e.currentTarget.y+=ay;
if(Math.abs(dy)<=.2)
e.currentTarget.removeEventListener(Event.ENTER_FR AME,openMenu);

closeMenu();
I apply the inertia effect to the y axe of 'menu_mc' based on the value of the variable 'arriveY' (the menu closes)
var dy:Number=arriveY-e.currentTarget.y;
var ay:Number=dy*.2;
e.currentTarget.y+=ay;
if(Math.abs(dy)<=.2)
e.currentTarget.removeEventListener(Event.ENTER_FR AME,closeMenu);

Source files available at downloads section of this site



See you soon!

Attached Files
File Type: zip hitArea.zip (10.1 KB, 19 views)

__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 28-08-08 at 06:55..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)  
Old 03-10-07, 15:04
Junior Member
 
Join Date: Oct 2007
Posts: 16
Rep Power: 0
Abdul is on a distinguished road
Hello Flep and thanks a lot for the wonderful forum and tutorials. Really impressed by how well everything is organized and presented.

Btw ... i couldn't find this tutorials source file! anyways idea?

Hey thanks again and keep up the great work!

Regards,
Abdul
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-10-07, 15:08
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Hi there :) welcome here

Look at this link:
http://www.flepstudio.org/forum/down...?do=file&id=79
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-10-07, 16:49
Junior Member
 
Join Date: Oct 2007
Posts: 16
Rep Power: 0
Abdul is on a distinguished road
Hey ! Thank you dude!
That was quick ... and i've got it ... thanks a million!

Much obliged!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-10-07, 16:51
Junior Member
 
Join Date: Oct 2007
Posts: 16
Rep Power: 0
Abdul is on a distinguished road
When i try to open any of your flash files downloads, it gives me an "Unexpected file format" error msg! am i doing something wrong? :S
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-10-07, 18:45
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Are you using Flash CS3 version ?
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-10-07, 21:25
Junior Member
 
Join Date: Oct 2007
Posts: 16
Rep Power: 0
Abdul is on a distinguished road
Oops! Nope ... i am using Flash 8!

But am not sure, does a visitor (say to a page that has AS 3 code) need to install the latest flash player? (i guess so ... i mean since the old compiler won't recognize most of the new commands :S ... i think)


I want to be really good with action script ... any resources you recommend would be really great and appreciated.

Thanks dude! You are one helpful person.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 03-10-07, 21:28
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
You can't use these scripts with Flash 8. You need Flash CS3
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 03-10-07, 21:38
Junior Member
 
Join Date: Oct 2007
Posts: 16
Rep Power: 0
Abdul is on a distinguished road
OK thanks a lot mate ... am still looking trying to find a good place to start learning real action scripting and using external as files.

wish me luck and be generous whenever u can :>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 03-10-07, 21:39
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Your welcome
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Sprite.hitArea una proprietà di Actionscript 3.0 Flep Articoli e tutorials 12 03-09-08 12:50
Come inserire un MC in uno Sprite? Donovant Actionscript 3.0 base 3 11-05-08 10:50
When do we use Sprite instead of a MovieClip? xoxo Actionscript 3.0 newbies 1 27-03-08 10:18
dimensioni sprite acca2o Actionscript 3.0 base 1 19-03-08 09:47
hitArea arbitrariamente complessa da bitmapdata??? giovapaglia Actionscript 3.0 avanzato 4 01-10-07 11:49


All times are GMT. The time now is 12:40.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap