Link to home
Start Free TrialLog in
Avatar of yarmouth
yarmouth

asked on

Drag and drop

How do you create some dynamic actionscripting in a game for eg.
You have a few ball symbols on the stage and a symbol of a bag. You can only put 5 balls into the bag before you get a meassage that the bag is full. You move the balls into the bag using the startdrag and droptarget actions, the balls can also be removed by dragging them out of the bag again.

I know how to write this code its simple.
But how do you tell flash when the bag is full, and that no more balls can be put into the bag until a ball is dragged out. What will the code look like?????

Th closest example I could find was the "collision detection" example. But that only works if 2 objects collide not 5.

Please advise
 
Avatar of AlfaNoMore
AlfaNoMore

You will need a counter. Each drop will check the current value of this counter, and display message if counter = 5.

When a ball is dropped into the bag, add one more to the counter.

Quite simple really. No code, but you should be able to work it out. As you've done the rest of it!!!
Use this for a bit of help if you don't know actionScript.

I'm guessing that when the ball is moved over the bag, an action is called. Add this to that function.

Function DropBallsInBag
If iBallsInBag <= 5 Then
    iBallsInBag = iBallsInBag + 1
    '//Call RemoveBall function
Else
    '// Display your error message
End If
End Function
Avatar of yarmouth

ASKER

Looks promising, I will take a look and let you know.
And yes, I don't know much actionscripting.
I don;t know actionScript either. That's VB, but I think Flash 4 actionScript was VB based. Flash 5 is javaScript based, but should be able to convert that for you.
Hi yarmouth,

You may find the following links useful(esp the first one). Check them out and let me know..

1. http://www.flashkit.com/tutorials/Interactivity/Other/Drag_n-Chris_Ma-192/index.shtml

2. http://www.flashkit.com/tutorials/Dynamic_Content/Create_a-Bob_Skid-212/index.shtml

Best Rgds,
Shekar
Below is my drag and drop code, where do I slot your code??
Basically I'm saying:
Drag 'app1' and droptarget 'chip1' and get a correct message.
Otherwise return to original x and y position and give an error message. Now where do I put your code???
PS:('app1' is the ball, 'chip1 is the bag')


on (press) {
    _root.icons.card1bigdup.chip1.app1.startDrag(true);
}
on (release) {
    stopDrag ();
    if (getProperty("/icons/card1bigdup/chip1/app1", _droptarget) eq "/icons/cardtop/chip2")
        tellTarget ("_root.icons.correct") {
            gotoAndPlay (2);
        }
    } else {
        setProperty ("_root.icons.card1bigdup.chip1.app1", _x, "15");
        setProperty ("_root.icons.card1bigdup.chip1.app1", _y, "-127");
        tellTarget ("_root.icons.error") {
            gotoAndPlay (2);
        }
    }
}
on (release) {
   if (balls_in_bag < 5) {
       stopDrag ();
       if (getProperty("/icons/card1bigdup/chip1/app1", _droptarget) eq "/icons/cardtop/chip2")
           tellTarget ("_root.icons.correct") {
               gotoAndPlay (2);
           }
       } else {
           setProperty ("_root.icons.card1bigdup.chip1.app1", _x, "15");
           setProperty ("_root.icons.card1bigdup.chip1.app1", _y, "-127");
           tellTarget ("_root.icons.error") {
               gotoAndPlay (2);
           }
       }
   else {
       gotoAndPlay (your error message);
   }
}

That should do it. Make sure you declare/ create a variable balls_in_bag = 0 at the beginning of your movie, but don't loop back to this frame, else your current count will be nulled!!!
if (getProperty("/icons/card1bigdup/chip1/app1", _droptarget) eq "/icons/cardtop/chip2")
          tellTarget ("_root.icons.correct") {
              balls_in_bag += 1;
              gotoAndPlay (2);
          }
Just one thing.

on (release) {
  if (balls_in_bag < 5) {
      stopDrag ();

Where does the "balls_in_bag" come from?
Is it an instance name, mc name, how does flash know that I'm referencing my balls_in_the_bag?????

It's a variable. One of the actionScript commands is "set variable". Just use this on your very first frame. Set it to equal 0 (as an integer, not string. No quotes around it in the little box).
Your quote: "That should do it. Make sure you declare/ create a variable balls_in_bag = 0 at the beginning of your
movie, but don't loop back to this frame, else your current count will be nulled!!!"

Ok, I have done this. Does it matter if my mc IS only 1 frame long?
AlfaVoMore,

on (press) {
     _root.icons.card1bigdup.chip1.app1.startDrag(true);

on (release) {
  if (balls_in_bag < 5){
      stopDrag ();
      if (getProperty("/icons/card1bigdup/chip1/app1", _droptarget) eq "/icons/cardtop/chip2")
         tellTarget ("_root.icons.correct") {
             balls_in_bag += 1;
             gotoAndPlay (2);
         }          
      } else {
          setProperty ("_root.icons.card1bigdup.chip1.app1", _x, "15");
          setProperty ("_root.icons.card1bigdup.chip1.app1", _y, "-127");
          tellTarget ("_root.icons.error") {
              gotoAndPlay (2);
          }
      }
  else {
      gotoAndPlay ("_root.icons.error");
  }
}

OK this is what I have so far, don't know what's wrong because it won't let me back into normal mode as this code contains errors!
Where are they? What am I doning wrong? Please advise
AlfaNoMore!

Please help me!!!!!
Sorry, I was away at lunch!! Maybe it's this: balls_in_bag += 1; That's standard javascript. Try

balls_in_bag = balls_in_bag + 1;
This might cause probs also:

if (getProperty("/icons/card1bigdup/chip1/app1", _droptarget) eq "/icons/cardtop/chip2")
        tellTarget ("_root.icons.correct") {
            balls_in_bag += 1;
            gotoAndPlay (2);
        }          
     

Change to:

if (getProperty("/icons/card1bigdup/chip1/app1", _droptarget) eq "/icons/cardtop/chip2") {
    tellTarget ("_root.icons.correct") {
        gotoAndPlay (2);
    }          
    /:balls_in_bag = /:balls_in_bag + 1;
}

/:balls_in_bag means reference the variable balls_in_bag inside the root movie.
Just excuse me if I sound thick. But, where does this bit go??

on (press) {
    _root.icons.card1bigdup.chip1.app1.startDrag(true);

on (release) {
 if (balls_in_bag < 5){
     stopDrag ();
     if (getProperty("/icons/card1bigdup/chip1/app1", _droptarget) eq "/icons/cardtop/chip2")
        tellTarget ("_root.icons.correct") {
                   gotoAndPlay (2);
   }          
   _root.icons.cardtop.chip2.balls_in_bag = _root.cardtop.chip2.balls_in_bag + 1;
}

        }          
     } else {
         setProperty ("_root.icons.card1bigdup.chip1.app1", _x, "15");
         setProperty ("_root.icons.card1bigdup.chip1.app1", _y, "-127");
         tellTarget ("_root.icons.error") {
             gotoAndPlay (2);
         }
     }
 else {
     gotoAndPlay ("_root.icons.error");
 }
}

This is what I have, but is it correct?
I have included the set variable balls_in_bag to 0 in the first frame. You spoke of a:
Function DropBallsInBag earlier up, what is this, have I left it out? Sorry for sounding ignorant, but I'm  a designer and know very little about coding in ANY language.
You could send me the Flash file if you want!!!?

daniel.newman@bis-web.com
I've never seen coding like yours before. Is Flash 5 writing that for you?

I would have "/icons/card1bigdup/chip1/app1" instead of your "_root.icons.card1bigdup.chip1.app1"
Trust me you won't have the time or patience to wade throu my file, it's huge and consists of about 100 different movie clips, all on different levels calling all sorts of things. Couldn't ask that of you.....

Ok just answer one question for the 100 points.
How do you create a counter.
Or maybe you know a tutorial?
Nope that's my dodgey code. But it's just flash 5 syntax. The / way is flash 4
ASKER CERTIFIED SOLUTION
Avatar of AlfaNoMore
AlfaNoMore

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
and all that on(press/release) code goes on the button. inside your ball movie, double click on the button, and view it's actions. Stick it in there.
Boy oh boy, is this going on and on :)!!!

BTW, yarmouth, did you check out the link or no??

Shekar.
He's given how to declare the variable, how to enter the stuff into the windo wand all that, with screenshots. This will help you a long way in putting in all the code that you are talking about.

Shekar.
Am I on long drive(Laborday weekend)?

I enjoyed this question thanks you both yarmouth, AlfaNoMore.

Cheers
RootDir
Thanks for all your help AlfaNoMore!
Didn't quite get all you did, but I'm sure the answer's in there somewhere......