#1 (permalink)  
Old 09-10-07, 18:27
Administrator
Living At The FlepStudio!
 
Join Date: Jul 2007
Location: Cesenatico
Posts: 4,917
Rep Power: 6
Flep is on a distinguished road
StartDrag with Flash CS3 - Actionscript 3.0

Here is another small change that happened from Actionscript 2.0 to Actionscript 3.0 .
The method startDrag of the MovieClip class has been ( in my opinion ) significantly improved without major changes.
Here are those changes?
Within Actionscript 2.0, you could control the drag of a MovieClip using, in fact, startDrag with its 5 parameters :
lock center: true or false, specifies if, while dragging the MovieClip, its centre corresponds to the Mouse centre ( true ) or the point where the click happened ( false ).
left: leftmost drag point
top: uppermost drag point
right: rightmost drag point
bottom: lowermost drag point

Within Actionscript 3.0, the drag is controlled by passing an instance of the Rectangle class to the startDrag method, which now accepts only 2 parameters:
lock center: as in Actionscript 2.0
bounds: Rectangle class instance

In order to use the bounds parameter, it?s sufficient to create a virtual rectangle remembering that only 4 simple parameters are needed to create a Rectangle object:

x: the x coordinate where the rectangle begins
y: the y coordinate where the rectangle begins
width: the width of the rectangle
height: its height

So, as you can see in the class I?ve written:
Code:
package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	public class Controllo extends MovieClip
	{
		private var rect:Rectangle;
		
		public function Controllo()
		{
			init();
		}
		
		private function init():void
		{
			stage.frameRate=31;
			
			ball_mc.x=sfondo_mc.x;
			ball_mc.y=sfondo_mc.y;
			ball_mc.buttonMode=true;
			
			initRectangle();
			initListener();
		}
		
		private function initRectangle():void
		{
			rect=new Rectangle(sfondo_mc.x-sfondo_mc.width/2+ball_mc.width/2,
							   sfondo_mc.y-sfondo_mc.height/2+ball_mc.height/2,
							   sfondo_mc.width-ball_mc.width,
							   sfondo_mc.height-ball_mc.height);
		}
		
		private function initListener():void
		{
			ball_mc.addEventListener(MouseEvent.MOUSE_DOWN,iniziaDrag);
			stage.addEventListener(MouseEvent.MOUSE_UP,stoppaDrag);
		}
		
		private function iniziaDrag(e:MouseEvent):void
		{
			ball_mc.startDrag(false,rect);
		}
		private function stoppaDrag(e:MouseEvent):void
		{
			ball_mc.stopDrag();
		}
	}
}
The result:







In this case, instead of creating a sort of virtual rectangle with new coordinates, I?ve passed the coordinates of sfondo_mc to the startDrag method.

Stay tuned !
__________________
_________________________________________
VIDEO CORSI ACTIONSCRIPT 3.0 creati da FlepStudio
I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread please !
- 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
sponsor links
Flashmint flash templates FlippingBook-PDF publisher Flash Media Server Hosting
sponsor links
Reply

Bookmarks

Tags
actionscript, cs3, flash, startdrag

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
[as2]startDrag Problemi DjRiKyX Flash CS4 3 18-12-08 16:19
Actionscript 3 startDrag,stopDrag etr102108 Actionscript 3.0 base 1 29-06-08 19:48
StartDrag multiplo lorenzz Flash Italiano 5 08-02-08 21:36
startDrag() StefanoV Actionscript 3.0 avanzato 2 06-10-07 18:25
startDrag controllato con Flash CS3 Flep Articoli e tutorials 0 19-09-07 13:31



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
vBulletin Skin developed by: vBStyles.com
FlepStudio 2007-2009