Flash CS5 AS3 Drag Drop Issue

Posted on 2012-09-04
Last Modified: 2016-08-29

I'm trying to produce a monopoly board style webpage with embedded flash.

I then need to get text values to show in the centre of the board as either:

> The mouse hovers over any of the 40 properties / boxes
> the token (top hat) is dragged over any of the 40 properties / boxes
> the token is dropped on any of the 40 properties / boxes

When any of the boxes are clicked they need to navigate to a hyper-link in an iframe.
This also needs to happen if the token is dropped on any of the boxes.

Problem 1:

> the token was dragging when I had only 3 test targets - but now I've put in all 40 it no longer moves - I'm not sure what is stopping it.

Would this be because I need to use variables to define all of the functions ratehr than listing them out manually?

Once this is resolved I'll post the rest of the problems as sepperate questions!

Attachment added here due to restrictions on main EE site: EE STUFF SITE

Question by:bedsingar
    LVL 39

    Accepted Solution

    have a look at this tutorial.

    i think your code could be simplified quite a bit.

    Author Comment


    thanks for the link, I've had a read through but as with most tutorials online this assumes one target. In my example I have 40 possible targets, so using code from the example above...

    "if (chevelle_mc.hitTestObject(truck_mc))" would I have to have an else if line for each of the 40 objects on on the stage?

    But looking at the example I can see my code should be an event listener on the stage rather than part of the function on the mouse up event.

    Author Comment

    Hi I've had a little more time to sit down and read about this now. I've restructured the code using data from the example link above and have got the drag drop working. However only for one target (40).

    Updated Flash Project

    I've tried to use the same looping that is used for the buttons on the board to produce a dynamic function rather than having a line for each target, but it looks like I've done something wrong here. As it is target 40 that works only I assume that I've written the function in a way that redefines the same function each time (i.E over writing it) when in fact I should be creating 40 distinct functions?

    I'm not sure how to change that though so I've still not managed to get it working. Any further help would be appreciated.


    Author Closing Comment

    After some further reading and a few separate questions I've managed to pull the answer together.

    Thanks for your contribution.

    Author Comment

    For the benefit of others here's the final code... it's my fist time using action script so I'm sure there are further improvements to be made.

    import fl.motion.MotionEvent;
    import flash.display.SimpleButton;
    import flash.display.Stage;
    import flash.display.MovieClip;
    import flash.display.Sprite; 
    import flash.sensors.Accelerometer;
    var IFRM_URL:URLRequest
    stop();//To pause on 1st frame
    //hide dice
    DA1.alpha = 100;
    DB1.alpha = 100;
    hidedice(null); // call function
    function hidedice(event:Event):void{
    DA2.alpha = 0;
    DA3.alpha = 0;
    DA4.alpha = 0;
    DA5.alpha = 0;
    DA6.alpha = 0;
    DB2.alpha = 0;
    DB3.alpha = 0;
    DB4.alpha = 0;
    DB5.alpha = 0;
    DB6.alpha = 0;
    //makesure that the user can select roll Dice
    StarText.mouseEnabled = false;
    // define starting position
    var ts:uint = 1 // target square
    // import the board text from an external text file
    //how to set and call a variable from outside the function :
    var testvar:Number = 0 //define variable outside of function
    function testfun():void {
    	testvar = 1 ;
    testfun(); // run function
    trace(testvar); // print value
    //update speach text
       Speach.text = ("Hi There!" + "\n" + "For a product and service update either click on ROLL DICE, or drag the token to your desired topic.");
    //rotate star
    stage.addEventListener(Event.ENTER_FRAME, rotatestar);
    function rotatestar(event:Event):void {
    	star.rotation += 1;
    //animation code
    function StopRotate(event:Event):void 
    	trace("function StopRotate Run");
        stage.removeEventListener(Event.ENTER_FRAME, rotatestar); 
    	//star.rotation += 0;
    //click star 
    star.addEventListener (MouseEvent.CLICK, ClickStar);
    var diceno:Array = [];
    function ClickStar(event:Event):void{
    	trace("star clicked");
    	//StarText.text = ("Rolling");
    	Speach.text = ("Rolling...");
    	diceno[0] = randomRange(1, 6);	
    	diceno[1] = randomRange(1, 6);	
    	var dice1:MovieClip  = getChildByName("DA" + diceno[0]) as MovieClip;
    	var dice2:MovieClip  = getChildByName("DB" + diceno[1]) as MovieClip;
    	//dice values 1 are left as 100% alpha so remove them first
    	DB1.alpha = 0;
    	DA1.alpha = 0;
    	//reset other dice
    	hidedice(null); // call function
    	//then show the rolled random dice
    	dice1.alpha = 100;
    	dice2.alpha = 100;
    	//update where the token should be now ... 
    	ts = ts + diceno[0] + diceno[1];
    	trace("Target Square Value = " + ts);
    	//check that we haven't exceeded the number of targets
    	if (ts > 40) {
    		ts  = ts - 40;
    	//move token
    	Token.x = getChildByName("target" + ts).x;
    	Token.y = getChildByName("target" + ts).y;
    	//update speach
    	Speach.text = ("You have rolled " + (diceno[0] + diceno[1]) + " and landed on: " + "\n" + lines[ts - 1] + " Click on Roll Dice when you are ready to move on." );
    	//start rotating star again 
    	stage.addEventListener(Event.ENTER_FRAME, rotatestar);
    	//update boardtext 
    	BoardText.text = (lines[ts - 1]);
    	//navigate to new URL
    	IFRM_URL = new URLRequest("pages/" + (ts) + ".html");
    	navigateToURL(IFRM_URL, 'IFRM01');
    	var minNum:uint = 1
    	var maxNum:uint = 6
    			/* The randomRange function */
    			function randomRange(minNum:Number, maxNum:Number):Number 
    				return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
    //load text file into array
    var csvLoader:URLLoader = new URLLoader(new URLRequest('boxtext.txt'));
    csvLoader.addEventListener(Event.COMPLETE, csvLoaded);
    var lines:Array
    function csvLoaded(event:Event):void{
       lines= String('\n');
       var linesNum:int = lines.length;
       for(var i:int = 0 ; i < linesNum; i++){
         trace('line ' + i + ': ' + lines[i]);
    // managing the token drag / drop process
    var startX:Number;
    var startY:Number;
    Token.addEventListener(MouseEvent.MOUSE_DOWN, TokenStart);
    function TokenStart(TokenMove:Event):void
    Token.addEventListener(MouseEvent.MOUSE_UP, TokenStop);
    function TokenStop(TokenMove:Event):void
    Token.buttonMode = true;
    var isTokenSelected:Boolean = false;
    var droppedTarget:int;
    Token.addEventListener(MouseEvent.MOUSE_DOWN, onTokenClick);
    Token.addEventListener(MouseEvent.MOUSE_UP, onTokenRelease);
    Token.addEventListener(MouseEvent.MOUSE_MOVE, onTokenMove);
    function onTokenClick(e:MouseEvent):void
    	isTokenSelected = true;
    function onTokenRelease(e:MouseEvent):void
    	isTokenSelected = false;
    	BoardText.text = (lines[droppedTarget - 1]) ;
    	IFRM_URL = new URLRequest("pages/" + droppedTarget + ".html");
    	navigateToURL(IFRM_URL, 'IFRM01');
    function onTokenMove(e:MouseEvent):void
    	if (isTokenSelected == true)
    		droppedTarget = 0;
    		for (var j:uint = 1; j <= 40; j++)
    			var targetid:SimpleButton = getChildByName("target" + j) as SimpleButton;
    			var tstring:String = "Target " + j;
    			if (targetid != null)
    				targetid.tabIndex = j;
    				if (Token.hitTestObject(targetid))
    					BoardText.text = (lines[j - 1]) ;
    					droppedTarget = j;
    for (var i:uint = 1; i <= 40; i++)
    	var button:SimpleButton = getChildByName("target" + i) as SimpleButton;
    	if (button != null)
    		button.tabIndex = i;
    		button.addEventListener(MouseEvent.CLICK, mouseClick);
    		//board text with tracer
    		button.addEventListener(MouseEvent.MOUSE_OVER, mouseOver);
    function mouseClick(event:MouseEvent):void
    	var index:uint = event.currentTarget.tabIndex;
    	var IFRM_URL:URLRequest = new URLRequest("pages/" + index + ".html");
    	navigateToURL(IFRM_URL, 'IFRM01');
    function mouseOver(event:MouseEvent):void
    	var index:uint = event.currentTarget.tabIndex;
    BoardText.text = (lines[index - 1]) ;

    Open in new window


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    json example 39 83
    Javascript Variable Through Modal Window 22 32
    CRM 2011 Decimal Out of Range 2 20
    Make code non clickable 6 28
    In Part 1 ( we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
    This article will show, step by step, how to integrate R code into a R Sweave document
    The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
    The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now