Action Script 3.0 Referencing multiple Arrays - actionscript-3

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];
}
}

Related

AS3 help needed if button1 of a movieclip && button2 of another movieclip clicked then gotoAndPlay

I made a memory cards game which has 24 card images from which the user has to find 7 images in order to win. I am a begginer of actionscript 3 all the code is on frames instead of document or class files. All 24 card images are movieclips, on the set of 7 images i made a button with the second state in the same layer with the win image. All the cards are created dynamically from the library and not in the stage.
My problem is what i ever have tried untill now for the last code in order to win had no success i'm very desperate please help me .....I cannot make two movieclips to communicate via variables.
enter image description here
Main timeline frame 6 has this code
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.*;
import flash.ui.Mouse;
import flash.utils.Timer;
import flash.utils.getTimer;
import flash.text.*;
import flash.display.Stage;
var objImage1:logo1 = new logo1;
var objImage2:pic2 = new pic2;
var objImage3:pic3 = new pic3;
var objImage4:logo2 = new logo2;
var objImage5:pic5 = new pic5;
var objImage6:pic6 = new pic6;
var objImage7:pic7 = new pic7;
var objImage8:logo3 = new logo3;
var objImage9:pic9 = new pic9;
var objImage10:pic10 = new pic10;
var objImage11:logo4 = new logo4;
var objImage12:pic12 = new pic12;
var objImage13:pic13 = new pic13;
var objImage14:pic14 = new pic14;
var objImage15:pic15 = new pic15;
var objImage16:logo5 = new logo5;
var objImage17:pic17 = new pic17;
var objImage18:logo6 = new logo6;
var objImage19:pic19 = new pic19;
var objImage20:pic20 = new pic20;
var objImage21:logo7 = new logo7;
var objImage22:pic22 = new pic22;
var objImage23:pic23 = new pic23;
var objImage24:pic24 = new pic24;
var sourceDeck:Array =[objImage1,objImage2,objImage3,objImage4,objImage5,objImage6,objImage7,objImage8,objImage9,objImage10,objImage11,objImage12,objImage13,objImage14,objImage15,objImage16,objImage17,objImage18,objImage19,objImage20,objImage21,objImage22,objImage23,objImage24];
var shuffledDeck:Array = new Array ();
var xValues:Array =["121","321","521","731","937","1136","120","320","520","732","938","1138","120","321","520","730","938","1135","121","322","520","732","938","1135"];
var yValues:Array =["173","173","173","173","173","173","346","346","346","346","346","346","534","534","534","534","534","534","716","716","716","716","716","716"];
function placeCards():void
{
for (var i:int = 0; i<sourceDeck.length; ++i)
{
addChild(sourceDeck[i]);
sourceDeck[i].x = int(xValues[i])+20;
sourceDeck[i].y = int(yValues[i]);
}
}
placeCards();
function removeCards():void
{
for (var i:int=0; i<sourceDeck.length; ++i)
{
removeChild(sourceDeck[i]);
}
}
function shuffleCards():void
{
while(sourceDeck.length >0)
{
var r:int=Math.floor(Math.random()*sourceDeck.length);
shuffledDeck.push(sourceDeck[r]);
sourceDeck.splice(r,1);
}
}
function placeShuffledCards():void
{
for (var i:int=0; i<shuffledDeck.length; ++i)
{
addChild(shuffledDeck[i]);
shuffledDeck[i].x = int(xValues[i]);
shuffledDeck[i].y = int(yValues[i]);
}
}
function showShuffledCards():void
{
removeCards();
shuffleCards();
placeShuffledCards();
}
showShuffledCards();
foo();
function foo():void {
var x:int = 2; // you can use variables as you would normally
// do something here
var timer:Timer = new Timer(5000);
var afterWaiting:Function = function(event:TimerEvent):void {
timer.removeEventListener(TimerEvent.TIMER, afterWaiting);
timer = null;
// 5 seconds have passed, now do something more
objImage1.gotoAndPlay(10);
objImage2.gotoAndPlay(10);
objImage3.gotoAndPlay(10);
objImage4.gotoAndPlay(10);
objImage5.gotoAndPlay(10);
objImage6.gotoAndPlay(10);
objImage7.gotoAndPlay(10);
objImage8.gotoAndPlay(10);
objImage9.gotoAndPlay(10);
objImage10.gotoAndPlay(10);
objImage11.gotoAndPlay(10);
objImage12.gotoAndPlay(10);
objImage13.gotoAndPlay(10);
objImage14.gotoAndPlay(10);
objImage15.gotoAndPlay(10);
objImage16.gotoAndPlay(10);
objImage17.gotoAndPlay(10);
objImage18.gotoAndPlay(10);
objImage19.gotoAndPlay(10);
objImage20.gotoAndPlay(10);
objImage21.gotoAndPlay(10);
objImage22.gotoAndPlay(10);
objImage23.gotoAndPlay(10);
objImage24.gotoAndPlay(10);
// the scope is retained and you can still refer to the variables you
// used earlier
x += 2;
}
timer.addEventListener(TimerEvent.TIMER, afterWaiting);
timer.start();
}
on each one of the seven buttons that each one is inside a movie clip I have this code:
import flash.display.Sprite;
import flash.text.*;
import flash.display.MovieClip;
import flash.events.MouseEvent;
var success:Boolean = false;
var butpic1:SimpleButton;
butpic1.enabled = true;
addChild(butpic1);
butpic1.addEventListener(MouseEvent.CLICK, clickpics);
function clickpics(event:MouseEvent):void {
gotoAndPlay(1);
var myTextBox:TextField = new TextField();
myTextBox.text = "Σωστό Συνέχισε!";
myTextBox.border = true;
myTextBox.borderColor = 0x000000;
myTextBox.width = 180;
myTextBox.height = 87;
myTextBox.x = -77;
myTextBox.y = -126;
var myFormat:TextFormat = new TextFormat();
myFormat.color = 000000;
myFormat.size = 24;
myFormat.align = TextFormatAlign.CENTER
myTextBox.background = true;
myTextBox.backgroundColor = 0xFFF000;
myTextBox.setTextFormat(myFormat);
addChild(myTextBox);
/*var Success=true;*/
//MovieClip(parent).Success2();
//trace(event.target.name);
var success=true;
if(MovieClip(root).butpic3 && butpic1 == success)
{
MovieClip(root).gotoAndPlay("win");
}
//checking();
//trace(event.target);
//MovieClip(this.parent).success2();
//checking();
//MovieClip(root).check();
//trace("click: " + event.currentTarget.name);
}
//trace(MovieClip(root).but.butpic2);
/*function checking():void
{
if(butpic1 && MovieClip(root).butt.butpic3)
{
MovieClip(parent).gotoAndPlay("win");
} else {
MovieClip(parent).gotoAndPlay("win");
}
}*/
/*function checking():void
{
if(MovieClip(root).Symbol1 && MovieClip(root).button2)
{
MovieClip(parent).gotoAndPlay("win");
}
}*/
I must warn you that putting code into MovieClips is an outmoded approach, and should be avoided. Putting code into different frames invariably causes more headaches. However, after reading through your code, I believe this should answer your question.
Note that most of the explanation is in the comments of the code, so read on.
import flash.text.*;
import flash.display.Stage;
/*You need a reference to each of these library instances, but you were creating
two references to them; (1) a variable, and (2) an array entry. You can create
this data once, thinking of each object as a individual "card", and add any
additional data to it together in the same place. Most importantly, this
structure allows us to use array.sortOn().*/
var deck:Array = [
{"img":new logo1, "valid":true},
{"img":new pic2, "valid":false},
{"img":new pic3, "valid":false},
{"img":new logo2, "valid":true},
{"img":new pic5, "valid":false},
{"img":new pic6, "valid":false},
{"img":new pic7, "valid":false},
{"img":new logo3, "valid":true},
{"img":new pic9, "valid":false},
{"img":new pic10, "valid":false},
{"img":new logo4, "valid":true},
{"img":new pic12, "valid":false},
{"img":new pic13, "valid":false},
{"img":new pic14, "valid":false},
{"img":new pic15, "valid":false},
{"img":new logo5, "valid":true},
{"img":new pic17, "valid":false},
{"img":new logo6, "valid":true},
{"img":new pic19, "valid":false},
{"img":new pic20, "valid":false},
{"img":new logo7, "valid":true},
{"img":new pic22, "valid":false},
{"img":new pic23, "valid":false},
{"img":new pic24, "valid":false}
];
/*You mentioned you were having difficulty getting your MovieClips to communicate
with eachother and track the success of valid clicks to "win". You can see we've
added a property "valid" which let's us know if it's a valid option for "winning".
We'll later add the state of it's "success".*/
// Because your locations were explicit pairs, it's also a good idea to keep these
// together, if nothing more than for maintainability & legibility.
var locs:Array = [
{"x":121, "y":173},
{"x":321, "y":173},
{"x":521, "y":173},
{"x":731, "y":173},
{"x":937, "y":173},
{"x":1136, "y":173},
{"x":120, "y":346},
{"x":320, "y":346},
{"x":520, "y":346},
{"x":732, "y":346},
{"x":938, "y":346},
{"x":1138, "y":346},
{"x":120, "y":534},
{"x":321, "y":534},
{"x":520, "y":534},
{"x":730, "y":534},
{"x":938, "y":534},
{"x":1135, "y":534},
{"x":121, "y":716},
{"x":322, "y":716},
{"x":520, "y":716},
{"x":732, "y":716},
{"x":938, "y":716},
{"x":1135, "y":716}
];
// Nulling of the timer & removal of the event listener wasn't strictly necessary.
// Simply use the second argument of Timer(millisecondDelay, repeatCount).
var timer:Timer = new Timer(5000, 1);
init();
function init():void {
// Here, we've replaced your foo() with an initialization function where all
// one-time stuff gets done.
timer.addEventListener(TimerEvent.TIMER, afterWaiting);
timer.start();
// Rather than checking within each card, we'll add the checks from the main stage.
// This also means you can get rid of the code inside the clips.
for each (var card:Object in deck) {
card.img.butpic1.addEventListener("click", checkCard);
}
showShuffledCards();
}
function showShuffledCards():void {
removeCards();
shuffleCards();
placeCards();
}
function removeCards():void {
for (var i:int = 0; i < deck.length; i++) {
// Ensure it needs to be removed, before attempting it.
if (deck[i].img.parent != null) {
removeChild(deck[i].img);
}
}
}
function shuffleCards():void {
/*Rather than keeping two arrays, and the shuffle only working once, we can
use sortOn() the existing array by changing the value of the property we're
sorting with. This effectively shuffles the deck, and allows us to shuffle
indefinitely.*/
for each (var card:Object in deck) {
card.order = Math.random();
}
deck.sortOn("order", Array.DESCENDING | Array.NUMERIC);
}
function placeCards():void {
// placeShuffledCards was doing the same thing as this function,
// so we simply use this one.
for (var i:int = 0; i < deck.length; i++) {
addChild(deck[i].img);
deck[i].img.x = locs[i].x + 20;
deck[i].img.y = locs[i].y;
}
}
function afterWaiting(event:TimerEvent):void {
// Rather than explicityly name each object, because we have them in a fancy
// array, we can write the command once, and call it on each card in the deck.
for each (var obj:MovieClip in deck) {
obj.gotoAndPlay(10);
}
}
function checkCard(e:Event):void {
// Search the deck for this card.
var successCount = 0;
for each (var card:Object in deck) {
if (e.currentTarget == card.img && card.valid) {
// If the card clicked is valid for winning,
// add the text and update its status.
card.img.gotoAndPlay(1);
var myTextBox:TextField = new TextField();
myTextBox.text = "Σωστό Συνέχισε!";
myTextBox.border = true;
myTextBox.borderColor = 0x000000;
myTextBox.width = 180;
myTextBox.height = 87;
myTextBox.x = -77;
myTextBox.y = -126;
var myFormat:TextFormat = new TextFormat();
myFormat.color = 000000;
myFormat.size = 24;
myFormat.align = TextFormatAlign.CENTER
myTextBox.background = true;
myTextBox.backgroundColor = 0xFFF000;
myTextBox.setTextFormat(myFormat);
card.img.addChild(myTextBox);
card.success = true;
}
// While we're looping through the deck, we'll also count up the number
// of successful clicks.
if (card.hasOwnProperty("success") && card.success == true) {
successCount++;
}
}
// If the count of successful clicks is 7, go to the "win" frame.
if (successCount == 7) {
gotoAndPlay("win")
}
}
Addendum
Since you haven't posted many questions yet, you may not be familiar with StackOverflow's rules/habits. When posting code, please update your question with that content. It's easier to read, and can be formatted (unlike comments).
How to debug
Beyond checkmarking "Permit Debugging" in the publish settings & holding control-shift-enter to launch in debugging mode, all you must do is familiarize yourself with the frames available; notably "Call Stack", "Variables", "Output", and your syntax editor.
In this case, you can see the program stopped in the init() function on line 90. The "Output" window is telling us that something doesn't exist, and from what's listed on 90, that'd have to be card, img, logo1, or checkCard.
Now, we know, the first two exist, and the last one exists, but logo1 (and specifically in at this location) does not exist. On the left you can see the variables available to the current scope of the code at this point in the program's runtime. We can dig inside of card and find the img property, and from here we can indeed confirm there is not logo1 property inside of img. Pretty simple once you know how the windows work, right? :)
As you did share you document, (and I'll repeat, because you're relatively new to SO), I've taken the liberty of fixing your fla file. I've also taken it a step further to show you where you might take your code next, removing the frame code, and creating your functionality/User-interface entirely from code with zero embedded images. You can download it from Dropbox.

AS3 Cannot access a property or method of null object reference, issue with gotoAndPlay

so I'm still fairly new to AS3 and I'm getting my head around the errors and stuff and I'm trying to figure out why this is not working how it should, the function still executes but it leaves an error on my console. Here is my code:
import flash.events.MouseEvent;
import flash.display.MovieClip;
stop();
var activeHitArray:Array = new Array(word_select_box);
var activeDropArray:Array = new Array(answer1_word, answer2_word, answer3_word);
var hitPositionsArray: Array = new Array();
for (var numi:int = 0; numi < activeDropArray.length; numi++) {
activeDropArray[numi].buttonMode = true;
activeDropArray[numi].addEventListener(MouseEvent.MOUSE_DOWN, mousedown1);
activeDropArray[numi].addEventListener(MouseEvent.MOUSE_UP, mouseup1);
hitPositionsArray.push({xPos:activeDropArray[numi].x, yPos:activeDropArray[numi].y});
}
function mousedown1(event:MouseEvent):void {
event.currentTarget.startDrag();
setChildIndex(MovieClip(event.currentTarget), numChildren - 1);
}
function mouseup1(event:MouseEvent):void {
var dropindex1:int = activeDropArray.indexOf(event.currentTarget);
var target:MovieClip = event.currentTarget as MovieClip;
target.stopDrag();
if(target.hitTestObject(activeDropArray[dropindex1])){
// target.x = activeHitArray[dropindex1].x;
//target.y = activeHitArray[dropindex1].y;
if(answer1_word.hitTestObject(word_select_box)){
gotoAndStop("6");
}
} else {
target.x = hitPositionsArray[dropindex1].xPos;
target.y = hitPositionsArray[dropindex1].yPos;
}
}
And the Error I'm getting through is:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at game_flv_fla::MainTimeline/frame6()
at flash.display::MovieClip/gotoAndStop()
at game_flv_fla::MainTimeline/mouseup1()
The only thing I can think of is that it is something to do with the gotoAndPlay() because when I place a trace into it's place I get no errors.
I copied your code, compiled and lauched it in Flash IDE. It works! :)
There were no errors.
But I know where the isue may lay. The addEventListeners are still on, no matter if there still are the objects they were linked to. You need to clean all active things before going to next frame:
if(target.hitTestObject(activeDropArray[dropindex1])){
if(answer1_word.hitTestObject(word_select_box)){
for (var i:uint = 0; i < activeDropArray.length; i++) {
activeDropArray[i].removeEventListener(MouseEvent.MOUSE_DOWN, mousedown1);
activeDropArray[i].removeEventListener(MouseEvent.MOUSE_UP, mouseup1);
}
gotoAndStop("6");
}
}

1120: Access of undefined property shuffledArray

Please can you help me out I am new to as3 and I am trying to create a shuffled deck using the Fisher-Yates Algorithm. When I run the code with ctrl-enter it compiles with no errors but when I try to output it with trace(); it comes back with:
Scene 1, Layer 'actions', Frame 1, Line 6 1120: Access of undefined property shuffledArray.
Like I said I am new to this and it will be me doing something very stupid but all the same i'm stuck.
Here is the code
package src.CardDeck
{
public class CardDeck
{
public var allCards:Array = [];
public var cardNames:Array;
public var cardValues:Array;
public var gameType:String;
public var drawnCards:uint = 0;
public function CardDeck(game:String)
{
gameType = game;
cardNames = ["Ace","Two","Three",
"Four","Five","Six",
"Seven","Eight","Nine",
"Ten","Jack","Queen","King"];
if(gameType == "texasholdem")
{
cardValues = [1,2,3,4,5,6,7,8,9,10,10,10,10];
}
makeSuit("Spade");
makeSuit("Heart");
makeSuit("Diamond");
makeSuit("Club");
}
function makeSuit(suitString:String):void
{
var card:Object;
for(var i:uint = 0; i < cardNames.length; i++)
{
card = {};
card.cardType = suitString;
card.cardName = cardNames[i];
card.cardValue = cardValues[i];
card.isDrawn = false;
allCards.push(card);
}
}
public function shuffleFisherYates():Array
{
var shuffledArray:Array = [];
var randomCardIndex: int;
do
{
randomCardIndex = Math.floor(Math.random()* allCards.length);
shuffledArray.push(allCards[randomCardIndex]); // add to mix
allCards.splice(randomCardIndex,1); // remove from deck
}while(allCards.length); // Meaning while allCards.length != 0
return shuffledArray;
}
}
}
and here is the .fla actions layer
import src.CardDeck.CardDeck;
var deck:CardDeck = new CardDeck("texasholdem");
trace(shuffledArray);
I know its probably something silly but i'm struggling.
Thanks in advance!
Paul
var deck:CardDeck = new CardDeck("texasholdem");
trace(shuffledArray);
This doesn't work because shuffledArray isn't defined there.
Try :
var deck:CardDeck = new CardDeck("texasholdem");
var array:Array = deck.shuffleFisherYates();
for(var i:int=0; i<array.length; i++)
{
trace(array[i].cardName);
trace(array[i].cardType);
trace(array[i].cardValue);
trace(array[i].isDrawn);
}
"shuffledArray" is a property inside of your CardDeck object. To access public methods and properties within it, you need to use the dot syntax:
trace(deck.shuffleFisherYates());
However, depending on what you are doing, you may not need to really be accessing the array directly, if your CardDeck object is meant to control the entire deck.

as3 error loading "good" "bad" array

I am creating a "good" "bad" array to display on screen which i can later use simple if statements to do something upon if the player has collided with the "good" "bad" objects.
I cant get the objects to randomly generate on screen with the following code.
// Create and Set good/bad random Word objects
public function newObject(e:Event)
{
var goodObjects:Array = ["WordObject1"];
var badObjects:Array = ["WordObject2"];
if (Math.random() < .5)
{
var r:int = Math.floor(Math.random()*goodObjects.length);
var classRef:Class = getDefinitionByName(goodObjects[r]) as Class;
var newObject:MovieClip = new classRef();
newObject.typestr = "good";
} else
{
r = Math.floor(Math.random()*badObjects.length);
classRef = getDefinitionByName(badObjects[r]) as Class;
newObject = new classRef();
newObject.typestr = "bad";
}
newObject.x = Math.random();
newObject.y = Math.random();
addChild(newObject);
objects.push(newObject);
placeWords();
}
// create random Word objects
public function placeWords() {
objects = new Array();
for(var i:int=0;i<numWordObjects;i++) {
// loop forever
while (true) {
// random location
var x:Number = Math.floor(Math.random()*mapRect.width)+mapRect.x;
var y:Number = Math.floor(Math.random()*mapRect.height)+mapRect.y;
// check all blocks to see if it is over any
var isOnBlock:Boolean = false;
for(var j:int=0;j<blocks.length;j++) {
if (blocks[j].hitTestPoint(x+gamesprite.x,y+gamesprite.y)) {
isOnBlock = true;
break;
}
}
// not over any, so use location
if (!isOnBlock) {
newObject.x = x;
newObject.y = y;
newObject.gotoAndStop(Math.floor(Math.random()*1)+1);
gamesprite.addChild(newObject);
objects.splice(newObject);
break;
}
}
}
}
i get the following errors:
1119: Access of possibly undefined property x through a reference with static type Function.
1119: Access of possibly undefined property y through a reference with static type Function.
1067: Implicit coercion of a value of type Function to an unrelated type flash.display:DisplayObject.
Try renaming var x and var y to something else. Those are public properties in any class that extends as a display object (Sprite/MovieClip/Shape).

How to load an external image on a drag & drop object using AS3?

NEW MESSAGE - THE SOLUTION I ENDED UP APPLYING
This is what I used to load an external image into a movie clip and drag and drop.
The feedback part is pretty much the same from the old code.
var holdermc_Arr:Array = new Array(4);
for(var i:uint = 0; i < holdermc_Arr.length; i++)
{
holdermc_Arr[i] = this["panel" + (i+1) + "_mc"];
var myLoader:Loader = new Loader();
var fileRequest:URLRequest = new URLRequest("images/" + (i+1) + ".jpg");
myLoader.load(fileRequest);
holdermc_Arr[i].addChild(myLoader);
holdermc_Arr[i].addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
holdermc_Arr[i].addEventListener(MouseEvent.MOUSE_UP, dropIt);
holdermc_Arr[i].buttonMode = true;
}
var startX:Number;
var startY:Number;
var counter:Number = 0;
function pickUp(event:MouseEvent):void {
startX = event.currentTarget.x;
startY = event.currentTarget.y;
setChildIndex(MovieClip(event.currentTarget),numChildren-1);
event.currentTarget.startDrag();
reply_txt.text = "";
}
OLD MESSAGE BELLOW
This is my latest attempt with the current error I am getting.
I actually would like to work with the code of my previous attempt because to me is easier that way to add multiple draggable movie clips.
But whichever the code, what seems to be the fundamental problem is that I am not setting up the property correctly for the "Loader".
Let me know if a link to the example is needed.
The error is the following:
ReferenceError: Error #1069: Property dropTarget not found on
flash.display.Loader and there is no default value. at
cs5test_fla::MainTimeline/ReleaseToDrop()
The code is the following
var myLoader:Loader = new Loader();
panel1_mc.addChild(myLoader);
var url:URLRequest = new URLRequest("uploadedImages/photo_1.jpeg");
myLoader.load(url);
var startX:Number;
var startY:Number;
var counter:Number = 0;
panel1_mc.addEventListener(MouseEvent.MOUSE_DOWN, ClickToDrag);
function ClickToDrag(event:MouseEvent):void
{
panel1_mc.startDrag();
startX = event.target.x;
startY = event.target.y;
reply_txt.text = "";
event.target.parent.addChild(event.target);
}
stage.addEventListener(MouseEvent.MOUSE_UP, ReleaseToDrop);
function ReleaseToDrop(event:MouseEvent):void
{
panel1_mc.stopDrag();
var myTargetName:String = "target" + event.target.name;
var myTarget:DisplayObject = getChildByName(myTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
reply_txt.text = "Good Job!";
event.target.x = myTarget.x;
event.target.y = myTarget.y;
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, ClickToDrag);
event.target.removeEventListener(MouseEvent.MOUSE_UP, ReleaseToDrop);
event.target.buttonMode = false;
counter++;
} else {
reply_txt.text = "Try Again!";
event.target.x = startX;
event.target.y = startY;
}
if(counter == 1){
reply_txt.text = "Congrats, you're finished!";
}
}
OLDER MESSAGE BELLOW
I am still struggling.
I am new to all this so I am really tying the best I can to grasp it.
The ActionScript I am trying to work with is all the way at the bottom.
I'd really appreciate if someone tries to look at this code and tell me how can I load external images to each draggable movie clip with out getting the following error.
ReferenceError: Error #1069: Property startDrag not found on
flash.display.Loader and there is no default value. at
dragdropDilema_fla::MainTimeline/pickUp() ReferenceError: Error #1069:
Property stopDrag not found on flash.display.Loader and there is no
default value. at dragdropDilema_fla::MainTimeline/dropIt()
I tried to use a simple var loader but for what I can see on the error, I have to set up the startDrag property to work with the Loaded image and not with the draggable movie clips that are currently in place??
I thought I would have just been simple if I had use the following for each:
var myLoader:Loader = new Loader();
imageHolder.addChild(myLoader);
var url:URLRequest = new URLRequest("uploadedImages/photo_1.jpeg");
myLoader.load(url);
and then do something like:
square_mc.imageHolder.addChild(myLoader);
but when I do that I get the error anyway when I click on the image.
Here is the entire code: It is supposed to count when the object meet their target and gives a message all through out and at the end.
panel1_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
panel1_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
panel1_mc.buttonMode = true;
var startX:Number;
var startY:Number;
var counter:Number = 0;
function pickUp(event:MouseEvent):void {
startX = event.target.x;
startY = event.target.y;
event.target.startDrag(true);
reply_txt.text = "";
event.target.parent.addChild(event.target);
}
function dropIt(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){
reply_txt.text = "Good Job!";
event.target.x = myTarget.x;
event.target.y = myTarget.y;
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
event.target.buttonMode = false;
counter++;
} else {
reply_txt.text = "Try Again!";
event.target.x = startX;
event.target.y = startY;
}
if(counter == 1){
reply_txt.text = "Congrats, you're finished!";
}
}
Thanks.
import flash.display.DisplayObjectContainer;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
var _dragMc:MovieClip = new MovieClip();
addChild(_dragMc);
loadImage("image.jpg")
function loadImage(path:String):void
{
var _loader:Loader = new Loader();
var _loaderToLoad:URLRequest = new URLRequest(path);
_loader.load(_loaderToLoad);
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded, false, 0, true);
}
function imageLoaded(evt:Event):void
{
addLoader(evt.currentTarget.loader, _dragMc);
}
function addLoader(loader:Loader, container:DisplayObjectContainer):void
{
container.addChild(loader);
addListeners();
}
function addListeners():void
{
_dragMc.addEventListener(MouseEvent.MOUSE_DOWN, setDrag, false, 0, true);
}
function setDrag(evt:MouseEvent):void
{
_dragMc.addEventListener(MouseEvent.MOUSE_MOVE, doDrag, false, 0, true);
_dragMc.removeEventListener(MouseEvent.MOUSE_DOWN, setDrag);
}
function doDrag(evt:MouseEvent):void
{
_dragMc.startDrag(false, null);
stage.addEventListener(MouseEvent.MOUSE_UP, endDrag, false, 0, true);
_dragMc.removeEventListener(MouseEvent.MOUSE_MOVE, doDrag);
}
function endDrag(evt:MouseEvent):void
{
_dragMc.stopDrag();
_dragMc.addEventListener(MouseEvent.MOUSE_DOWN, setDrag, false, 0, true);
stage.removeEventListener(MouseEvent.MOUSE_UP, endDrag);
}
This loads an image onto a MovieClip called _dragMc. The rest from there is up to you. I also added some quick code that will enable you to drag _dragMc around.