Drag and Drop Button within MovieClip [Help thanks ^^]
I'm Having a Problem on how to make things work here.
Here's the Idea:
I'm doing a Game Drag and Drop (Food Chain Game). I want it more attractive by having cool animations such as:
1.) If the Object(Animal) is being hovered, an animation occurs such as Animal is being Frightened.
2.) If the Object(Animal) is being Clicked&Drag, an animation again occurs such as an Animal is trying to get loose; the Fish is like taken from water or Others.
3.) If then the Object is dropped onto any Boxes(Which I made an Array of Boxes), the Animal will likely to have been caught and Jailed and so whatever.
also if the Object is not being Clicked/Hovered or etc. It will animate himself whatever animation I will make to him. thanks ^^
That Idea made me think of Using a Button Inside a MovieClip, but when I tried doing it, Errors Came up and the MovieClip isn't draggable. also Errors appears like:
flash.display::Sprite/constructChildren();
flash.display::Sprite()
flash.display::MovieClip; and etc.
I know little on how to animate those things but my Problem is, I can't make it Work:yikes:
Any Help will be greatly APpreciated, You are my Only Chance. I can't attach my File because it's over the 300kb limit..
Here's my External ActionScript for PublicClass:
Code:
package Scripts
{
import flash.events.MouseEvent;
import flash.utils.Dictionary;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.*;
import flash.display.Sprite;
import fl.transitions.Tween;
import fl.transitions.easing.*;
dynamic public class PublicClass extends MovieClip {
public var dict:Dictionary = new Dictionary();
public var correct = 0;
public var error = 0;
public var failed_attempt = 0;
public var hits:int = 0;
public var max:int = 0;
public var maxCount:int = 0;
public var droppedTargets:Array = new Array();
public function PublicClass()
{
}
// Listeners **************
public function mouseDownHandler(evt:MouseEvent):void {
var object = evt.target;
parent.addChild( object );
object.startDrag();
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}
public function mouseUpHandler(evt:MouseEvent):void {
var obj = evt.target;
var targetTrap = false;
obj.stopDrag();
for (var i:int = 0; i < dropTargets.length; i++) {
var target:MovieClip = dropTargets[i] as MovieClip;
if (target.hitTestObject(obj) == true) {
if (alreadyBeenDraggedCheck(target) == false) {
droppedTargets.push(target);
targetTrap = true;
test_match(target,obj);
}
}
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}
if (targetTrap == false) {
obj.x = obj.initial_x;
obj.y = obj.initial_y;
}
}
function test_match(target,obj) {
if (dict[target] == obj)
{
hits = hits+1;
max = max+1;
trace("Yes ! You got one !");
obj.alpha = 0.5;
obj.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
obj.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
centerMe (obj, target);
}
else
{
max = max+1;
failed_attempt = failed_attempt + 1;
trace("Missed :(");
obj.alpha = 0.5;
obj.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
obj.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
centerMe (obj, target);
}
if(max == maxCount)
{
button_btn.visible = true;
}
if(max > 0)
{
reset_btn.visible = true;
}
}
function centerMe(dragged:MovieClip,droppedOn:MovieClip):void {
var startX = dragged.x;
var startY = dragged.y;
var destX = dragged.x = droppedOn.x + ((droppedOn.width - dragged.width)/2);
var destY = dragged.y = droppedOn.y + ((droppedOn.height - dragged.height)/2);
var myXtween:Tween = new Tween(dragged, "x", Elastic.easeOut, startX, destX, 1, true);
var myYtween:Tween = new Tween(dragged, "y", Elastic.easeOut, startY, destY, 1, true);
}
function alreadyBeenDraggedCheck(dropCandidate:MovieClip):Boolean {
var alreadyDraggedOn = false;
for (var i=0;i<droppedTargets.length;i++) {
if (droppedTargets[i] == dropCandidate) {
alreadyDraggedOn = true;
}
}
return alreadyDraggedOn;
}
// Pindutan **************
function Next(evt:MouseEvent):void {
if (hits == maxCount)
{
correct = correct + 1;
trace("Made it !!");
}
else
{
trace("You Made an Error!!");
error = error + 1;
}
gotoAndStop(frame = frame + 1);
max = 0;
hits = 0;
for each (var item in dict)
item.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler)
}
function nextStage(evt:MouseEvent):void {
gotoAndStop(frame = frame + 1);
correct = 0;
error = 0;
failed_attempt = 0;
}
}
}
here's the Code in the Internal Timeline per Problem
Code:
dict[box1_mc] = flower_mc;
dict[box2_mc] = cater_mc;
dict[box3_mc] = bird_mc;
var dropTargets:Array = [box1_mc, box2_mc, box3_mc];
frame = 4;
maxCount = 3;
button_btn.visible = false;
reset_btn.visible = false;
button_btn.addEventListener(MouseEvent.CLICK, Next);
reset_btn.addEventListener(MouseEvent.CLICK, Reset);
dict[box1_mc].initial_x = dict[box1_mc].x;
dict[box1_mc].initial_y = dict[box1_mc].y;
dict[box2_mc].initial_x = dict[box2_mc].x;
dict[box2_mc].initial_y = dict[box2_mc].y;
dict[box3_mc].initial_x = dict[box3_mc].x;
dict[box3_mc].initial_y = dict[box3_mc].y;
function Reset(evt:MouseEvent):void {
dict[box1_mc].x = dict[box1_mc].initial_x;
dict[box1_mc].y = dict[box1_mc].initial_y;
dict[box2_mc].x = dict[box2_mc].initial_x;
dict[box2_mc].y = dict[box2_mc].initial_y;
dict[box3_mc].x = dict[box3_mc].initial_x;
dict[box3_mc].y = dict[box3_mc].initial_y;
droppedTargets.splice(0);
flower_mc.alpha = 1;
cater_mc.alpha = 1;
bird_mc.alpha = 1;
for each (var item in dict)
item.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
max = 0;
hits = 0;
reset_btn.visible = false;
button_btn.visible = false;
}