Link to home
Start Free TrialLog in
Avatar of bedsingar
bedsingarFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Flash CS5 AS3 Drag Drop Issue

Hello,

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

Thanks
ASKER CERTIFIED SOLUTION
Avatar of blue-genie
blue-genie
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bedsingar

ASKER

Hi,

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.
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.

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

Thanks for your contribution.
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 flash.events.MouseEvent;
import fl.motion.MotionEvent;
import flash.net.URLRequest;
import flash.display.SimpleButton;
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
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");
	StopRotate(null);
	//StarText.text = ("Rolling");
	Speach.text = ("Rolling...");
	diceno[0] = randomRange(1, 6);	
	diceno[1] = randomRange(1, 6);	

	trace(diceno[0]);
	trace(diceno[1]);

	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(event.target.data).split('\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.startDrag();
}

Token.addEventListener(MouseEvent.MOUSE_UP, TokenStop);
function TokenStop(TokenMove:Event):void
{
	Token.stopDrag();
	Token.stopDrag();
}

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