Object showing back at frame 1 - actionscript-3

Hi I'm new to flash and as3 and have very basic knowledge with as3, I'm trying to create a simple drag and drop games, my goal the games need to be work as a stack layering objects like making a hamburger ( sorry I don't know how to say this but as below pictures )
My Timeline
My actions for frame 1
stop();
bantenbiakaon.addEventListener(MouseEvent.CLICK, bantenbiakaonquiz);
function bantenbiakaonquiz ( event:MouseEvent):void
{
gotoAndStop(2);
}
Actions at frame 2
stop();
import flash.events.MouseEvent;
import flash.display.DisplayObject;
var objectoriginalX:Number;
var objectoriginalY:Number;
sidi.buttonMode = true;
sidi.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
sidi.addEventListener(MouseEvent.MOUSE_UP, dropObject);
function pickupObject(event:MouseEvent):void
{
event.target.startDrag();
event.target.parent.addChild(event.target);
objectoriginalX = event.target.x;
objectoriginalY = event.target.y;
}
function dropObject(event:MouseEvent):void
{
event.target.stopDrag();
var matchingTargetName:String = "target" + event.target.name;
var matchingTarget:DisplayObject = getChildByName(matchingTargetName);
if(event.target.dropTarget != null && event.target.dropTarget.parent == matchingTarget)
{
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
event.target.removeEventListener(MouseEvent.MOUSE_UP, dropObject);
event.target.buttonMode = false;
event.target.x = matchingTarget.x;
event.target.y = matchingTarget.y;
gotoAndStop(3);
}
else
{
event.target.x = objectoriginalX;
event.target.y = objectoriginalY;
}
}
Actions at frame 3 will drag and drop to target and then open frame 4
Actions at frame 4 a Symbol MouseEvent gotoAndPlay(1); to frame 1
My problem when all object has been put into the target when the Symbol Button click back to frame 1, the object still appear. How actually to fix my problem.
Problem screenshot at frame 1

Related

Actionscript 3 drag and drop multiple objects to target with well done

The drag and drop works, however, I have no idea how to create an if statement that goes to the next scene when all movieclips have been placed on the target.
I've tried placing the instance names in an if statement with the hittestobject however, no luck.
import flash.events.TouchEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.display.MovieClip;
/* Touch and Drag Event
Allows the object to be moved by holding and dragging the object.
*/
var objectoriginalX:Number;
var objectoriginalY:Number;
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
var lemons:Array = [lemon1_mc, lemon2_mc, lemon3_mc, lemon4_mc, lemon5_mc];
for each(var lemonMC:MovieClip in lemons)
{
lemonMC.buttonMode = true;
lemonMC.addEventListener(TouchEvent.TOUCH_BEGIN, pickobject);
lemonMC.addEventListener(TouchEvent.TOUCH_END, dropobject);
lemonMC.startX = lemonMC.x;
lemonMC.startY = lemonMC.y;
}
var fl_DragBounds:Rectangle = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
function pickobject(event:TouchEvent):void
{
event.target.startTouchDrag(event.touchPointID, false, fl_DragBounds);
event.target.parent.addChild(event.target);
objectoriginalX = event.target.x;
objectoriginalY = event.target.y;
}
function dropobject(event:TouchEvent):void
{
if(event.target.hitTestObject(target_mc)){
event.target.buttonMode = false;
event.target.x = target_mc.x;
event.target.y = target_mc.y;
event.target.visible = false;
} else {
event.target.x = event.target.startX;
event.target.y = event.target.startY;
event.target.buttonMode = true;
}
}
var melons:Array = [melon1_mc, melon2_mc, melon3_mc, melon4_mc, melon5_mc, melon6_mc, melon7_mc];
for each(var melonMC:MovieClip in melons)
{
melonMC.buttonMode = true;
melonMC.addEventListener(TouchEvent.TOUCH_BEGIN, pickobject2);
melonMC.addEventListener(TouchEvent.TOUCH_END, dropobject2);
melonMC.startX = melonMC.x;
melonMC.startY = melonMC.y;
}
var fl_DragBounds2:Rectangle = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
function pickobject2(event:TouchEvent):void
{
event.target.startTouchDrag(event.touchPointID, false, fl_DragBounds2);
event.target.parent.addChild(event.target);
objectoriginalX = event.target.x;
objectoriginalY = event.target.y;
}
function dropobject2(event:TouchEvent):void
{
if(event.target.hitTestObject(target_null)){
event.target.buttonMode = false;
event.target.x = target_mc.x;
event.target.y = target_mc.y;
event.target.visible = false;
} else {
event.target.x = event.target.startX;
event.target.y = event.target.startY;
event.target.buttonMode = true;
}
}
How about adding a counter that is equal to number of objects to drag, then every time you drop object (and detect if it was on target) you decrements the counter and at the end of the function you check if it's 0?
An easy way to do this would be to remove your lemons/melons from their arrays when they pass the hit test. Then check if each array is empty and continue to the next scene should that be the case.
You can actually reduce redundant code and use the same function (dropobject) for both lemons and melons.
function dropobject(event:TouchEvent):void {
//Figure out which array this belongs to (is it a lemon or a melon)
var array:Array; //the array the dropped item belongs to
var hitMC:MovieClip; //the hit object for the lemon or melon
if(lemons.indexOf(event.currentTarget) > -1){ //if the lemon array contains the currentTarget
array = lemons;
hitMC = target_mc;
}else{
array = melons;
hitMC = target_null;
}
if(event.currentTarget.hitTestObject(hitMC)){
event.currentTarget.buttonMode = false;
event.currentTarget.x = hitMC.x;
event.currentTarget.y = hitMC.y;
event.currentTarget.visible = false;
//remove the item from it's array
array.removeAt(array.indexOf(event.currentTarget));
//check if there are any items left
if(lemons.length < 1 && melons.length < 1){
//both arrays are empty, so move on
play(); //or however you want to move on
}
}
}
Getting more advanced, a better way to do this would be to make a base class for your lemons, melons and anything else you want to drag in the future. Then you can add the dragging functionality into that base class and add properties for the hit target and an event for when it's hit it's target. This would give you one code base that can be easily applied to any library object.

Next button in drag and drop button doesnt function

I am trying to create a drag and drop game. I want to create a next button and it didnt work. If I remove the next button, the drag and drop game works fine but once I add the drag and drop button, then the whole game doesnt function. Here is my code. Can anyone help me?
import flash.events.MouseEvent;
var objectOriginalX:Number;
var objectOriginalY:Number;
answer.buttonMode = true;
answer.addEventListener(MouseEvent.MOUSE_DOWN, pickObject);
answer.addEventListener(MouseEvent.MOUSE_UP, dropObject);
answer1.buttonMode = true;
answer1.addEventListener(MouseEvent.MOUSE_DOWN, pickObject);
answer1.addEventListener(MouseEvent.MOUSE_UP, dropObject);
answer2.buttonMode = true;
answer2.addEventListener(MouseEvent.MOUSE_DOWN, pickObject);
answer2.addEventListener(MouseEvent.MOUSE_UP, dropObject);
answer3.buttonMode = true;
answer3.addEventListener(MouseEvent.MOUSE_DOWN, pickObject);
answer3.addEventListener(MouseEvent.MOUSE_UP, dropObject);
next_btn.buttonMode = true;
next_btn.addEventListener(MouseEvent.CLICK, next_btn);
/*next_btn.addEventListener(MouseEvent.CLICK,next_btn)
next_btn.buttonMode = true;*/
/*stop();
function next_btn(event:MouseEvent):void
{
gotoAndStop(5);
}*/
function pickObject(event:MouseEvent):void
{
event.target.startDrag();
event.target.parent.addChild(event.target);
objectOriginalX = event.target.x;
objectOriginalY = event.target.y;
}
function dropObject (event:MouseEvent):void
{
event.target.stopDrag();
var matchingTargetName:String = event.target.name + "Target" ;
var matchingTarget: DisplayObject = getChildByName(matchingTargetName);
if(event.target.dropTarget != null && event.target.dropTarget.parent == matchingTarget)
{
event.target.removeEventListener(MouseEvent.MOUSE_DOWN,pickObject);
event.target.removeEventListener(MouseEvent.MOUSE_UP,dropObject);
event.target.buttonMode = false;
event.target.x = matchingTarget.x;
event.target.y = matchingTarget.y;
}
else {
event.target.x = objectOriginalX;
event.target.y = objectOriginalY;
}
function next_btn.MovieClip(event:MouseEvent):void
{
gotoAndStop(5);
}
}
stop();

Action Script 3.0 Referencing multiple Arrays

I'm attempting to create a Jigsaw Puzzle in Flash cs6 (Actionscript 3.0), here is my code:
import flash.events.Event;
import flash.events.MouseEvent;
addEventListener(Event.ENTER_FRAME,onenter);
stop();
function pickupObject(event:MouseEvent):void
{
event.target.startDrag(true);
}
function dropObject(event:MouseEvent):void
{
event.target.stopDrag();
}
function dropTarg(event:MouseEvent):void
{
event.target.stopDrag(x,y);
}
function onenter(event:Event)
{
var pieces = [p1,p2,p3,p4,p5,p6];
var targets = [target1,target2,target3,target4,target5,target6]
var targ = [p1.targ1,p2.targ2,p3.targ3,p4.targ4,p5.targ5,p6.targ6]
var xcoord = [241.00,374.40,529.85]
var ycoord = [224.65,224.65,224.65]
for each (var i in pieces)
{
i.buttonMode = true;
i.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
i.addEventListener(MouseEvent.MOUSE_UP, dropObject);
if (targets[i].hitTestObject(i.targ[i]))
{
//i.removeEventListener(MouseEvent.MOUSE_UP,dropObject);
i.x = xcoord[i];
i.y = ycoord[i];
//i.addEventListener(MouseEvent.MOUSE_UP,dropObject);
}
}
}
What I am trying to achieve is basically, if user clicks on say p1 (this object has another known as targ within its layers) and that targ hits the corresponding target, then the object will snap into play at x, y coordinate.
The error I am getting is
TypeError: Error #1010: A term is undefined and has no properties.
at JigSawWithArrays_fla::MainTimeline/onenter()
Which leads me to believe I am not referencing/accessing the arrays correctly.
Is this doable? I am a newbie to Flash cs6 and would appreciate some guidance on this.
Thanks in advance.
Try for instead of for each
for, for each difference
for (var i:int = 0; i < pieces.length; i++) {
var piece:MovieClip = pieces[i] as MovieClip;
piece.buttonMode = true;
piece.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
piece.addEventListener(MouseEvent.MOUSE_UP, dropObject);
if (targets[i].hitTestObject(targ[i]))
{
piece.x = xcoord[i];
piece.y = ycoord[i];
}
}

Flash video in movieclip problems

I have several video files playing on the stage. I've converted them into movieclips so that I can scale and drag them by clicking. The problem is that I cannot loop them.
Then I tried to make them as SWF playback object's but after that my code wasn't working with them.
Next step was to make them Embedded video objects so that they loop automatically and the code is working. After that there appeared a problem that the objects are duplicating at some point.
Here's the original code as the videos are movieclips.
var allDraggables:Array = new Array();
var mouseHold = false;
stage.addEventListener(MouseEvent.MOUSE_UP, mUp);
function mUp(MouseEvent)
{
mouseHold = false;
}
function draggableObject(mc)
{
var mouseOnThisObject = false;
allDraggables.push(mc);
mc.addEventListener(Event.ENTER_FRAME, drag);
mc.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
function mDown(MouseEvent)
{
mouseHold = true;
mouseOnThisObject = true;
}
function drag(MouseEvent)
{
if (mouseHold == true && mouseOnThisObject == true)
{
mc.addEventListener(Event.ENTER_FRAME, dragger);
}
if (mouseHold == false)
{
mc.removeEventListener(Event.ENTER_FRAME, dragger);
mouseOnThisObject = false;
}
}
mc.doubleClickEnabled = true;
mc.addEventListener(MouseEvent.DOUBLE_CLICK, scaleMe);
function scaleMe(e:MouseEvent)
{
if (e.target.scaleX < 2)
{
e.target.scaleX= e.target.scaleY = 2;
}
else (e.target.scaleX= e.target.scaleY = 1);
}
function dragger(Event)
{
mc.x+=(mouseX-mc.x)/3;
mc.y+=(mouseY-mc.y)/3;
for (var i:int=0; i<allDraggables.length; i++){
if(mc.hitTestObject(allDraggables[i]) && getChildIndex(allDraggables[i]) > getChildIndex(mc)){
swapChildren(allDraggables[i], mc)
}
}
}
}
draggableObject(green);
draggableObject(red);
draggableObject(video1);
draggableObject(video2);
draggableObject(video3);
Well it's hard to tell what you've tried exactly, since you haven't provided any code (yet)..
However, from the top of my head, I think this will work:
if(videoMC1.currentFrame == 250) { //put the number of the last frame of the movieclip in place of 250
loopMC();
}
function loopMC() {
videoMC1.stop();
videoMC1.gotoAndPlay(1);
}
What you do here is simple; you check the current frame that is passed/playing and when it reaches the desired number (in your case most likely the last frame) it calls a function that resets and plays the video.
I found some old code I once used in a project, maybe you could try this. In my flash app it worked, although I didn't put the video into a movieclip.
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.NetStatusEvent;
var nc:NetConnection = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
ns.bufferTime = 10;
var vid:Video = new Video(1024,640);
vid.attachNetStream(ns);
addChild(vid);
ns.addEventListener(NetStatusEvent.NET_STATUS, ns_onPlayStatus)
function ns_onPlayStatus(event:NetStatusEvent):void {//loop video
if(event.info.code == "NetStream.Play.Stop") {
ns.seek(0);
}
}

multiple draggable item into a single target AS3

I'm new to AS3 and was wondering if there is anyone out there that can help me with this...
I'm creating a drag and drop Flash activity where there is 1 target and multiple draggable items. In my case I have 4 apples and I want to be able to put all apples into the same basket. I can get the draggable item into one target but i cannot get multiple draggable items into one single target...
Here is my code...
**************************************************************
import caurina.transitions.*;
//import flash.geom.Rectangle;
//var myBoundaries:Rectangle=new Rectangle(68.65,637.8,100,50);
circle1_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
circle1_mc.addEventListener(MouseEvent.MOUSE_UP, drop);
function drag(event:MouseEvent):void {
//event.target.startDrag(true, myBoundaries);
event.target.startDrag();
}
function drop(event:MouseEvent):void {
event.target.stopDrag();
var myTargetName:String = "target" + event.target.name;
var myTarget:DisplayObject = getChildByName(myTargetName);
if(event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
//trace("hit");
/*Remove the event listeners when a peg is correctly placed*/
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, drag);
event.target.removeEventListener(MouseEvent.MOUSE_UP, drop);
event.target.buttonMode = false;
/*Adjust the peg’s position when it is correctly placed*/
event.target.x = myTarget.x;
event.target.y = myTarget.y;
/*add tween*/
Tweener.addTween(circle1_mc,{x:68.65,y:637.8,time:1,transition:"easeIn"});
} else {
//trace("try again");
/*add tween*/
Tweener.addTween(circle1_mc,{x:97.9,y:64.95,time:1,transition:"easeIn"});
}
}
circle1_mc.buttonMode = true;
**************************************************************
Hope to hear from you soon.
Perhaps wrapping the apples in to an appleContainer ( draggableItemContainer? ) then instead of dragging single apples you'll be able to focus on the appleContainer as your target?