Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Action Script 3 Drag & Drop

Explorer ,
Oct 29, 2010 Oct 29, 2010

Hi,

I have been following a Lynda tutorial to generate a simple drag and drop game in Flash CS5, using Action Script 3 and have run into what I am sure for somebody will be a simple problem to solve; I have got to the stage where I have 10 movie clips and 10 relating _target areas to drop these clips onto, these then snap into place in the correct area.

My problem is that I have a counter running in the background which is counting the number of correct conversions; what I want is when all 10 items have been dropped in the correct area (relative _target) a line of Action Script will be executed, now for this example I would like to bring the Alpha of a textfield/movie clip on the stage from 0 - 1, showing a win message.

Here is my code so far, I am using external classes for the process of this drag and drop.

Grid Class:

package
{

    import flash.display.*;
import flash.events.*;


public class Grid extends MovieClip
{
  var dragdrops:Array;
  var numOfMatches:uint = 0;


  public function Grid()
  {
   // constructor code
   dragdrops = [moneya,moneyb,moneyc,moneyd,moneye,moneyf,moneyg,moneyh,moneyi,moneyj];
   var currentObject:DragDrop;
   for(var i:uint = 0; i < dragdrops.length; i++)
   {
    currentObject = dragdrops;
    currentObject.target = getChildByName(currentObject.name + "_target");
   
   }
  }
 
  public function match():void
 
  {
   numOfMatches++;
   if(numOfMatches == dragdrops.length)
   {
    HERE IS WHERE I WANT TO PUT ALPHA CODE
   }
  }
 
}

}

DradDrop Class:

package
{
import flash.display.*;
import flash.events.*;

public class DragDrop extends Sprite
{
 
  var origX:Number;
  var origY:Number;
  var target:DisplayObject;

  public function DragDrop()
  {
   // constructor code
   origX = x;
   origY = y;
   addEventListener(MouseEvent.MOUSE_DOWN, drag);
   buttonMode = true;
  }
 
  function drag(evt:MouseEvent):void
  {
   stage.addEventListener(MouseEvent.MOUSE_UP, drop);
   startDrag();
   parent.addChild(this);
  }
 
  function drop(evt:MouseEvent):void
  {
   stage.removeEventListener(MouseEvent.MOUSE_UP, drop);
  
   stopDrag();
  
   if(hitTestObject(target))
   {
    visible = false;
    target.alpha=1;
    Object(parent).match();
   }
  
   x = origX;
   y = origY;
  }

}

}

Any help would be great.

TOPICS
ActionScript
3.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 01, 2010 Nov 01, 2010

use:


package
{

    import flash.display.*;
import flash.events.*;


public class Grid extends MovieClip
{
  var dragdrops:Array;
  var numOfMatches:uint = 0;


  public function Grid()
  {
   // constructor code
   dragdrops = [moneya,moneyb,moneyc,moneyd,moneye,moneyf,moneyg,moneyh,moneyi,money j];
   var currentObject:DragDrop;
   for(var i:uint = 0; i < dragdrops.length; i++)
   {
    currentObject = dragdrops;
    currentObject.target = getChildByName(currentObject.name + "_target");
   
   }
  }
 
  public fu

...
Translate
Community Expert ,
Oct 29, 2010 Oct 29, 2010

if items correctly dropped can no longer be dragged, just increment a counter when an object is correctly dropped and check if the counter has reached 10.

if correctly dropped items can be redropped on the correct target, it will be a little more complex.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 01, 2010 Nov 01, 2010

Yes Kglad, that is what I am trying, I have a counter variable running called ' numOfMatches' which is counting everytime one of my movie clips makes successful contact with the correct '_target zone'.

No items can be redropped, once they are placed on the correct _target then their button status is deactivated, I would just like to test if this is counting succesfully by calling a code to check if all 10 have been counted and if this is true then to bring a text element's Alpha from invisible to visible following all my movieclips being placed in the correct places.

What code could I try, I have a high proficency in Director's LINGO language but am struggling to transfer this over. In Lingo I would probably write something as simple as:

if  gNumOfMatches=10 then
     
      sprite("wintxt").blend=sprite("wintxt").blend+100
     
    end if

This would result in my text element sprite's Alpha going from 0 (invisible) to 100 (fully visible).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2010 Nov 01, 2010

use:


package
{

    import flash.display.*;
import flash.events.*;


public class Grid extends MovieClip
{
  var dragdrops:Array;
  var numOfMatches:uint = 0;


  public function Grid()
  {
   // constructor code
   dragdrops = [moneya,moneyb,moneyc,moneyd,moneye,moneyf,moneyg,moneyh,moneyi,money j];
   var currentObject:DragDrop;
   for(var i:uint = 0; i < dragdrops.length; i++)
   {
    currentObject = dragdrops;
    currentObject.target = getChildByName(currentObject.name + "_target");
   
   }
  }
 
  public function match():void
 
  {
   numOfMatches++;
   if(numOfMatches == dragdrops.length)
   {

// use a dynamic textfield so you can assign an instance name (eg, tf) and use:
tf.alpha=1;
   }
  }
 
}

}

DradDrop Class:

package
{
import flash.display.*;
import flash.events.*;

public class DragDrop extends Sprite
{
 
  var origX:Number;
  var origY:Number;
  var target:DisplayObject;

  public function DragDrop()
  {
   // constructor code
   origX = x;
   origY = y;
   addEventListener(MouseEvent.MOUSE_DOWN, drag);
   buttonMode = true;
  }
 
  function drag(evt:MouseEvent):void
  {
   stage.addEventListener(MouseEvent.MOUSE_UP, drop);
   startDrag();
   parent.addChild(this);
  }
 
  function drop(evt:MouseEvent):void
  {
   stage.removeEventListener(MouseEvent.MOUSE_UP, drop);
  
   stopDrag();
  
   if(hitTestObject(target))
   {
    visible = false;
    target.alpha=1;
    Object(parent).match();
   }
  
   x = origX;
   y = origY;
  }

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 02, 2010 Nov 02, 2010

Thanks Kglad, this worked, I had written this code previously, however I am using TLF text and so nothing was happening and I didn't understand why, I didn't realise that you had to change it to Dynamic Text for this to work, the only snag is that I am not using one of the default fonts, so what I have done instead is just go to a Frame Label when all 10 items are placed correctly and sort of cheated to bring the congratulations text up, but this method is cool all the same for future projects.

I have an old tutorial on outputting different text strings using Dynamic Text also which I could incorporate in future projects, it is just a shame that the Classic Text in Flash outputs such a strange, almost blurred version of the font!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 02, 2010 Nov 02, 2010
LATEST

your welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines