ActionScript 3 : Where’s the _root?

Where is those _root.VariableName in ActionScript 3.0?

Good question, well _root is still there, but in ActionScript 3 is usually replaced with stage.variable_name, stage.objectname or MovieClip(”name”) commands.

A neat way to make a reference to _root is as follow. E.g. calling a _root variable called score and update it to 10.

1
2
//Accessing _root variables and object names in Actionscript 3.0
MovieClip("root").score += 10;

AS 3 : HitTest Checks on ActionScript 3.0

This is an example on how one should do a hittest in ActionScript 3.0.

This is an example of plain simple flash mode, where MovieClipA(red) and MovieClipB(blue) - Red being draggable and blue square listens to the a HitTest condition.

Some criteria needed for this task :-

  • Two movieclips - Red and Blue
  • EventListener assigned to Red to have it drag and drop able.
  • EventListener for EnterFrame for Blue to constantly checks for HitTest condition

For those new in EventListener in ActionScript 3.0 here’s a good tutorial for it.

See sample below :-


Download sample source code here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//Task to accomplish for HitTest
//1. Two movieclips - Red and Blue (Given instance name MovieClipRed and MovieClipBlue
//2. EventListener assigned to Red to have it drag and drop able.
//3. EventListener for EnterFrame for Blue to constantly checks for HitTest condition
 
 
//Task 1. is done in the stage itself.
//Task 2. Place an Eventlistener on Red have it drag nad drop able.
MovieClipRed.addEventListener(MouseEvent.MOUSE_DOWN,startDragme);
MovieClipRed.addEventListener(MouseEvent.MOUSE_UP,stopDragme);
 
// Below is the trigger function for EventListener Task 2.
function startDragme(event:Event):void{
	MovieClipRed.startDrag(true);
	StatusTxt.text = "Dragging Red";
}
 
function stopDragme(event:Event):void{
	MovieClipRed.stopDrag();
	StatusTxt.text = "Dropping Red";
}
 
MovieClipBlue.addEventListener(Event.ENTER_FRAME,checkHitTest);
 
function checkHitTest(event:Event){
		if(MovieClipRed.hitTestObject(MovieClipBlue))
		{
			StatusTxt.text = "Bingo - Blue has detected Red";
		}
}

AS3: Scripting OnClick GetURL? (Where’s GetURL)

Another big suprise for AS3 is the missing GetURL where we used to script it on buttons to open up url. However that was replaced in ActionScript 3.0. We use navigateToURL instead.

Just follow the example when scripting your button. Remember like in previous tutorials, you need to add an eventlistener to your button to attached a function to it.

1
2
3
4
5
6
this.YourButton.addEventListener(MouseEvent.CLICK,openURL);
function openURL(event: MouseEvent)
{
	var request:URLRequest = new URLRequest("http://www.isgoodstuff.com");
	navigateToURL(request);
}

Writing A Musical Loop in ActionScript 3.0

musicloop Sorry for the late posting, I’ve been buzy with my new IPOD lately :3. Today’s tutorial shows how to construct a musical loop in Action Script 3.0. There’s also a mute and un-mute button just in case many of you wondering how to do it.

Completing this tutorial you can some of you presentation animators (those that requires Action Script sound control on presentation loop) can resume your project without much worrying.

Is still almost the same as AS 2.0. The syntax may change a little bit. You can do much more now, they upgraded these sound object stuff.

Here take a look at the sample and scripted code below.

  Read the rest of this entry »

AS3: Scripting OnClick Play Sound

This is the most common scripting found for playing sound in ActionScript 3.0. In ActionScript 3.0 they have this Sound Channel which represent a sound object, with it you can have a MUCH BETTER sound control in Flash.

Today’s tutorial in we going to create a simple movieClip , attach an click event listener on it and it triggers the function to play a sound object from the library.

 

Read the rest of this entry »