AS3 Game Inventory Pages - actionscript-3

I'm new to AS3 and decided to make a simple drag&drop decor game just to get started. My problem is that I can't figure out on how to make another page of items when I click on the arrow; also navigating through the categories of items.
Here is a sample of the game SWF
One more question. I'm using this code for every item. Is there a way to make this code more compact instead of copying&pasting the code for every item?
var Clone1:MovieClip;
Ground01.addEventListener(MouseEvent.MOUSE_DOWN, GroundPressed);
function GroundPressed(event:MouseEvent):void
{
Clone1 = new ground01();
Clone1.x = 132;
Clone1.y = -123;
addChild(Clone1);
Clone1.startDrag();
Clone1.addEventListener(MouseEvent.MOUSE_DOWN,onClonedPlusPressed1);
}
function onClonedPlusPressed1(event:MouseEvent):void
{
Clone1 = MovieClip(event.currentTarget);
Clone1.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, onStageReleased1);
function onStageReleased1(event:MouseEvent):void
{
if(Clone1 != null){
Clone1.stopDrag();
}
if(Clone1.hitTestObject(Trashcan)) {
removeChild(Clone1);
Clone1 = null;
}
}

Assuming you are new to action script 3 and programming, in any programming language you can always reusing functions, take your cloning GroundPressed() function for example:
function GroundPressed(event:MouseEvent):void
{
Clone1 = new ground01();
Clone1.x = 132;
Clone1.y = -123;
addChild(Clone1);
Clone1.startDrag();
Clone1.addEventListener(MouseEvent.MOUSE_DOWN,onClonedPlusPressed1);
}
You can always reuse this function and apply to each of your movieclips
like so:
Ground01.addEventListener(MouseEvent.MOUSE_DOWN, GroundPressed);
Ground02.addEventListener(MouseEvent.MOUSE_DOWN, GroundPressed);
Ground03.addEventListener(MouseEvent.MOUSE_DOWN, GroundPressed);
If you want to make your code even more compact and save writing the extra EventListener() you can group Ground01, Ground02, Ground03 into one huge movieclip apply a single EventListener(). To point to the right child you will need e.target(). I can't remember the actual syntax but your code will look something like below:
function GroundPressed(event:MouseEvent):void
{
cloneItem = event.target;
cloneItem.x = 132;
cloneItem.y = -123;
addChild(cloneItem);
cloneItem.startDrag();
cloneItem.addEventListener(MouseEvent.MOUSE_DOWN,onClonedPlusPressed1);
}
You can read more about event.target here.

Related

What's tangling instances of a createjs Sprite together?

We're converting Animate projects to HTML5, and it's dumping out code that creates multiple instances of a Sprite-type object. It's generating sprite definitions like this:
(lib.quarterback = function() {
this.spriteSheet = ss["Page12_Canvas_atlas_"];
this.gotoAndStop(11);
}).prototype = p = new cjs.Sprite();
And, those being used multiple times like this:
// Quarter Back copy 1
this.instance_19 = new lib.quarterback();
this.instance_19.parent = this;
this.instance_19.setTransform(-16.1,-371.1);
this.instance_19._off = true;
this.timeline.addTween(cjs.Tween.get(this.instance_19) /* snip! */ .to({_off:true},1).wait(51));
// Quarter Back copy 2
this.instance_20 = new lib.quarterback();
this.instance_20.parent = this;
this.instance_20.setTransform(-16.1,-371.1);
this.instance_20._off = true;
this.timeline.addTween(cjs.Tween.get(this.instance_20) /* snip! */ .to({_off:true},1).wait(51));
// Quarter Back copy 3
this.instance_21 = new lib.quarterback();
this.instance_21.parent = this;
this.instance_21.setTransform(-16.1,-371.1);
this.instance_21._off = true;
this.timeline.addTween(cjs.Tween.get(this.instance_21) /* snip! */ .to({_off:true},1).wait(51));
// etc.
Everything form the start of these instances on the timeline, up through the snipped portion in the example, works OK. However, the only one instance of each Sprite (quarterback in this case) is successfully being removed from the timeline at/near the end. The others simply get "stuck" in the their last position.
We've proven that these instances are OK on their own -- removing all but any one of them shows the expected behavior. But, having more than one instance of any given Sprite seems to prevent them all from being removed.
What's going on here?!
The following example will toggle the target off on frame 0, and then back on for frame 1. You can use the "visible" property to achieve the same effect.
var tween = createjs.Tween.get(target).to({_off:false}).wait(1).to({_off:true}).wait(1).to({_off:false});
In above code that target is a graphics, to hide/show we are using _off.
Even i got this issues i have solve by calling a function and hide the movieclip
this.timeline.addTween(cjs.Tween.get(this.instance_21).to({_off:true},1).wait(51).call(hideInstance,this));
function hideInstance()
{
this.instance_21.visible =false;
}

Unable to reference MovieClip inside Button AS3

I have this annoying issue that I hope someone might be able to help me with.
I have a mute button that I created and I have another movieclip inside of that button. All I want it to do is when I toggle the mute the movieclip inside will go to the according frame.
However, every time I try to call the movieclip inside of the button, this error comes up:
Access of possibly undefined property mcMuteToggle through a reference with static type flash.display:SimpleButton.
The instance name for the movieclip within is "mcMuteToggle".
Why not make movieClips that act like buttons?? Since I dont think actual button (simpleButton) types can deal with sub-MovieClips (especially if they too have code). Even if possible don't do it, I can predict a mess whereby Button does things it shouldn't do depending on what code you have in those MClips.
Try an alternate button method, just for a test... You didnt show any test code to work with so I will make assumptions..
1) Make a shape (rectangle?) and convert to MovieClip (or if all coded, then addchild shape to new MovieClip). Let's assume you called it mc_testBtn.
2) Make that MC clickable by coding mc_testBtn.buttonMode = true;
3) Add your mcMuteToggle inside the mc_testBtn
(or by code: mc_testBtn.addChild(mcMuteToggle);
Now you can try something like..
mc_testBtn.addEventListener (MouseEvent.CLICK, toggle_Mute );
function toggle_Mute (evt:MouseEvent) : void
{
if ( whatever condition )
{
mc_testBtn.mcMuteToggle.gotoAndStop(2); //go frame 2
}
else
{
mc_testBtn.mcMuteToggle.gotoAndStop(1); //go frame 1
}
}
This is likely due to strict mode. You can either disable it in the ActionScript settings dialog, access it with a different syntax myButton['mcMuteToggle'], or make a class for the symbol that includes a property mcMuteToggle.
You can also check to make sure the symbol is actually on the stage and that clip is actually in the button:
if('myButton' in root) {
// ...
}
if('mcMuteToggle' in myButton) {
// ...
}
i think u just overwrite that codes. You u can use something like this:
var soundOpen:Boolean = true;
var mySound:Sound = new Sound(new URLRequest("Whatever your sound is"));
var mySc:SoundChannel = new SoundChannel();
var mySt:SoundTransform = new SoundTransform();
mySc = mySound.play();
mcMuteToggle.addEventListener(MouseEvent.CLICK, muteOpenSound);
function muteOpenSound(e:MouseEvent):void
{
if(soundOpen == true)
{
mcMuteToggle.gotoAndStop(2);
/*on frame 2 u need to hold ur soundClose buton so ppl can see :)*/
soundOpen = false;
mySt.volume = 0;
mySc.soundTransfrom = st;
}
else
{
mcMuteToggle.gotoAndStop(1);
soundOpen = true;
mySt.volume = 1;
mySc.soundTransfrom = st;
}
}
This is working for me everytime. Hope u can use it well ;)

Can i create a function to tell a button to open a movie clip of the same name

I am new to the actionscript side of flash,
I am working on a map that has say 20 popups(movieclips) and the countries are the buttons, i have just been informed i need to add 60 more.
Below is an example of the code i have been using
english_movie.visible=french_movie.visible=turkish_movie.visible=false
english_btn.addEventListener(MouseEvent.CLICK, englishButtonClick);
french_btn.addEventListener(MouseEvent.CLICK, frenchButtonClick);
turkish_btn.addEventListener(MouseEvent.CLICK, turkishButtonClick)
function englishButtonClick(event:MouseEvent):void {
english_movie.visible=true;
english_movie.play();
french_movie.visible=turkish_movie.visible=false
}
function frenchButtonClick(event:MouseEvent):void {
french_movie.visible=true;
french_movie.play();
english_movie.visible=turkish_movie.visible=false
}
function turkishButtonClick(event:MouseEvent):void {
turkish_movie.visible=true;
turkish_movie.play();
english_movie.visible=french_movie.visible=false
}
Im thinking there must be an easier way to do this than replicating the code over and over.
Any help would be much appreciated.
Here's how to simplify the whole thing with code: Each btn object is
related to one movie object. This can be achieved with a Dictionary.
var btnToMovieAssociation:Dictionary = new Dictionary();
btnToMovieAssociation[english_btn] = english_movie; // repeat this line for every btn/movie pair
Now you have to generalise your click handler. The key difference
between each function (apart from making one certain movie visible)
is that they all make certain other movies invisible. But actually,
it's sufficient to only make the previously visible movie invisble.
To do this, create a variable that keeps track of the current visible
movie.
var currentMovie:MovieClip = english_movie;
Initialising the variable with english_movie has no effect on the
program. you can pick any other of the movies. It will make things
easier in the following code if this variable is initialised.
Now your function does effectively this:
make movie of clicked button visible
play this movie
make last movie invisible
Here's the cool part. You only add one listener. Look up if something
is in the dictionary for the clicked thing and consider that the
movie you want to show next.
addEventListener(MouseEvent.CLICK, buttonClick);
function buttonClick(event:MouseEvent):void
{
var movie:MovieClip = btnToMovieAssociation[event.target]
if (movie == null)
return; // nothing in the dictionary, it wasn't a button that was clicked.
movie.visible=true;
movie.play();
currentMovie.visible = false;
currentMovie = movie;
}
There are problems with this solution:
You still have to declare every pair, which is still tedious and prone to erro. (you have to type every name twice)
If your buttons are made up of several objects, event.target might point to them instead of the button as a whole. But with only
the btns in the dictionary and not all their individual parts,
nothing would be found in the dictionary. This can be circumvented by
setting mouseChildren = false; on every btn.
i posted this question else where and got this response
var tl:MovieClip=this;
var mc:MovieClip;
var i:int;
var buttonA:Array=[english_btn,french_btn,turkish_btn];
for(i=0;i<buttonA.length;i++){
buttonA[i].addEventListener(MouseEvent.CLICK,buttonF);
tl[buttonA[i].name.split("_")[0]+"_movie"].visible = false;
}
function buttonF(e:MouseEvent):void{
for(i=0;i<buttonA.length;i++){
tl[buttonA[i].name.split("_")[0]+"_movie"].visible = false;
}
tl[e.currentTarget.name.split("_")[0]+"_movie"].visible=true;
}
Which works great.

From synchronous flow to asynchronous flow dilemma

As a PHP programmer I'm very habituated to the fact that the program flow goes line by line, if I call function1() that have to return some value I know the program wont continue until that function makes a return.
Now, im developing a AS3 app using AIR, I need to download some images if some conditions are met, don't know how many, so im using a For like this:
for (var w:int=0; w < newData_array[2]['content'].length; w++ ) {
for (var j:int=0; j < oldData_array[2]['content'].length; j++ ) {
if((oldData_array[2]['content'][j]['id'] == newData_array[2]['content'][w]['id']) && (oldData_array[2]['content'][j]['last_mod'] != newData_array[2]['content'][w]['last_mod'])){
//need to download a image...
}
}
}
As you can see im just comparing each element from each array (newData_array and oldData_array). If the condition met, I need to download something, for that I'm using URLloader and as you know this function is asynchronous, adding a listener, an event will be triggered when the download is complete, the problem is very clear, since the urlloader wont stop the for cycle (like I would expect on a PHP alike language) I'm just going to download a bunch of images at the same time creating a disaster because I wont know when to save. So I need to use by any mean the listener, but since I'm not very habituated to this kind of procedures I'm pretty munch stuck.
Im not interested on the save to disk routines, that part is pretty much done, I just want to understand how I should structure this algorithm to make this work.
This annoyed me too, but one has to realize that you can't do something to something that doesn't exist in memory yet, and these loading operations can take quite some time. Being a single-threaded stack, Flash Player's load operations would require that the entire interface to freeze during the load (if restricted to the loop).
Obviously there's data available to the loop that dictates what you do with the image. The solution I've been using is create a custom load function that starts the load, and returns a proxy image . Your loop gets a container back and you can freely place your image where you want.
In the meantime, your loader listener has kept track of the active images loading, and their associated proxies. When loaded, swap the image in.
Your loop would look something like this...
for (var w:int in newData_array[2]['content']) {
for (var j:int in oldData_array[2]['content']) {
if ((oldData_array[2]['content'][j]['id'] == newData_array[2]['content'][w]['id']) && (oldData_array[2]['content'][j]['last_mod'] != newData_array[2]['content'][w]['last_mod'])){
var proxy:MovieClip = loadImage("image/path.jpg");
// Do stuff to proxy.
}
}
}
And your function + listener would look something like this...
var proxies:Object = {};
function loadImage(path:String):MovieClip {
// load Image
var proxy:MovieClip = new MovieClip();
proxies.path = proxy;
// Load Image
}
function imageLoaded(e:Event):void {
// Once loaded, use the filename as the proxy object's key to find the MovieClip to attach the image.
proxies[e.data.loaderInfo.filename].addChild(e.data);
}
This is just off the top of my head, so you'll need to find the actual variable names, but I hope that makes sense.
I am not really sure if I understood your problem but it seems to me that you simply add the according listener in your inner loop.
//take care this is pseudocode
var parent = ... // some stage object
for (var w:int=0; w < size1; w++ ) {
for (var j:int=0; j < size2; j++ ) {
if(some_condition){
request = new URLRequest(getNextImagePath();
urlLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, onComplete);
urlLoader.load(request);
}
}
}
function onComplete(event:Event) {
parent.addChild(createImageFromData(this.data));
}
I solved the problem using an third party library that pretty munch handles this problem.
https://github.com/arthur-debert/BulkLoader

Passing local variable to loader anonymous handler function

Why isn't this working as I am thinking it would:
var i:int=-1;
for each(obj in myData)
{
i++;
var loader:Loader=new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(event:Event)
{
trace(i);
});
}
There are 3 objects in myData and the trace statement looks like:
2
2
2
Instead of:
0
1
2
If I add i to an array (like myArr.push(i)) it will have 3 elements, 0, 1 and 2.
Any ideas?
Thank you.
That's a very bad approach you've taken... Just don't do any of those things you are trying to do, and it'll be fine... No point in using anonymous function here (it's never actually in AS3), no point to use for-each, because what you need is for(;;). You use dynamic typing for no benefit what so ever (there's no benefit in dynamic typing in AS3 and never was anyway). And, yeah, the closure will capture the context, the context has only one i, and it's value is 2, so the first trace is what you should expect.
What you should be doing - store the loaders in some data structure and fetch them from that data structure later (when you need that identifier). And please, for the sake of us users, load whatever you are trying to load sequentially - because if you don't, we'll get the IO errors you aren't handling...
First let me tell you why it doesn't work as you expect.
What is happening is, the for is looping through your elements, and creates all the loaders, incrementing i, but the Event.COMPLETE happens sometime later, where the i is already at the value 2, so that's why you get that output.
As wvxvw suggested, you need some more data structure, something like this:
class MyLoader {
private var i: int;
private var loader: Loader;
function MyLoader(i:int) {
this.i = i;
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
}
function onLoaded(event:Event)
{
trace(i);
}
}
And you will use it in your loop:
var i:int = 0;
for each(obj in myData) {
var loader:MyLoader=new MyLoader(i++);
}
Of course, you will need to add lots more to that MyLoader, like handling the errors, and pass more meaningful things to make everything work.