-
Dragging items into a target area and display a button
Hi,
Having problems in dragging three objects onto a target, which will make a button appear.
This is my code so far:
Code:
cookingarea.visible = false;
page2Button.visible = false;
milk.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_5);
function fl_ClickToDrag_5(event:MouseEvent):void
{
milk.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_5);
function fl_ReleaseToDrop_5(event:MouseEvent):void
{
milk.stopDrag();
}
eggs.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_7);
function fl_ClickToDrag_7(event:MouseEvent):void
{
eggs.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_7);
function fl_ReleaseToDrop_7(event:MouseEvent):void
{
eggs.stopDrag();
}
butter.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_9);
function fl_ClickToDrag_9(event:MouseEvent):void
{
butter.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_9);
function fl_ReleaseToDrop_9(event:MouseEvent):void
{
butter.stopDrag();
}
{
if(milk.hitTestObject(cookingarea) &&
eggs.hitTestObject(cookingarea) &&
butter.hitTestObject(cookingarea)){
page2Button.visible = true;
}
}
page2Button.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame);
function fl_ClickToGoToAndPlayFromFrame(event:MouseEvent):void
{
gotoAndPlay(101);
}
Thanks
steeashford
-
What error message are you getting?
-
This code is not in a function. It will execute when you come to the frame, before you have a chance to drag anything.
Code:
{
if(milk.hitTestObject(cookingarea) &&
eggs.hitTestObject(cookingarea) &&
butter.hitTestObject(cookingarea)){
page2Button.visible = true;
}
}
You need to run that code every time you release one of your draggable items so that you can check whether they are all in the right place after a drag. So put that code in a function, and call that function in each of the release functions. You can also refactor your release functions so that you only have one for all three cases. You can also refactor your start drag functions to reuse the same one three times as well.
Code:
cookingarea.visible = false;
page2Button.visible = false;
var dragObject:Sprite = null;
milk.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
eggs.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
butter.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
function clickToDrag(event:MouseEvent):void{
dragObject = Sprite(event.currentTarget);
dragObject.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
function releaseToDrop(event:MouseEvent):void{
if (dragObject != null){
dragObject.stopDrag();
dragObject = null;
checkDraggables();
}
}
function checkDraggables():void{
if(milk.hitTestObject(cookingarea) &&
eggs.hitTestObject(cookingarea) &&
butter.hitTestObject(cookingarea)){
page2Button.visible = true;
}
}
page2Button.addEventListener(MouseEvent.CLICK, clickToGoToAndPlayFromFrame);
function clickToGoToAndPlayFromFrame(event:MouseEvent):void{
gotoAndPlay(101);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|