Animate / Flash CC, AS3. Want to toggle button.upState on and off - actionscript-3

I'm working on a school project right now. I've searched far and wide and haven't found a solution.
I have 2 simpleButtons. One is named btOui, the other btNon. I was looking for a way to make btOui's downState be false when btNon's downState is true, and vice versa. I was using :
btNon.upState = btNon.downState;
// And
btNon.upState = btNon.upState;
But my understanding is that btNon's upState gets overridden by its downState. In a way, btNon's upState is now a copy of its downState, so I can never get its original upState again.
I hope I'm being clear. I'm not particularly in a hurry, but any answer is appreciated !

Try the following program
if(btNon.upState)
{
btNon.downState = false;
}
else
{
btNon.downState = true;
}

Related

Could somebody help me out by telling me how to convert Actionscript 2 to actionscript3?

I devised a simple game that is at mice using actionscript 3. I do not understand how to change the code of actionscript 2 to actionscript 3, please help me.
Could somebody help me out by telling me how to convert Actionscript 2 to actionscript3 ?
jumlah = 0;
waktu_muncul = 0;
function buat_tikus()
{
nomor = random(3)+1;
tikus=attachMovie("tikus","tikus",2{_x:_root["lingkaran"+nomor]._x,_y:_root["lingkaran"+nomor]._y});
tikus.onEnterFrame = function()
{
waktu_muncul++;
if (waktu_muncul == 0)
{
this.gotoAndStop(4);
}
if (waktu_muncul == 20)
{
removeMovieClip(this);
buat_tikus();
waktu_muncul = 2;
}
tikus.onPress = function()
{
sound1 = new Sound();
sound1.attachSound("Suaratikus");
sound1.start(0,1);
var tikus:MovieClip;
if (jumlah == 90)
{
stop();
nextFrame();
}
if (this._currentframe == 1)
{
jumlah += 10;
this.gotoAndPlay(2);
}
};
};
}
onEnterFrame = function ()
{
fps++;
if (fps == 20)
{
timer_txt--;
fps=0
if (timer_txt==0)
{
stop();
gotoAndStop(6);
}
}
palu.swapDepths(_root.getNextHighestDepth());
palu._x = _xmouse;
palu._y = _ymouse;
};
onMouseDown = function ()
{
if (palu._currentframe == 1)
{
palu.gotoAndPlay(2);
}
};
buat_tikus();
var fps : Number = 0;
var timer_txt : Number = 20;
BotMaster's response to this question is right on the money; not to belabor the point, but I have been in this person's shoes, and I can understand the frustration of migrating from AS2 to AS3. Long term: Learn AS3. Short Term: if you have this game, and somehow you need to make it work ASAP (learning AS3 will take some time), one thing you might try is to create an empty AS3 swf that loads your complete game. That might work (but like I said, long term AS3 is more powerful).
Also, depending on how you created the game, you might try the Google Swiffy extension which might convert some basic AS2 games. Note that it WONT convert as2 to as3 but it might make the game accessible on the web. Best of luck
To be clear on AS2/AS3 conversion. This is not possible, those are 2 different languages and their keywords/global methods/properties/event system/display list, etc ... do not match and for that reason are incompatible. That is why for example there's no tool that exist that can convert in one click of a button a AS2 code to a AS3 code.
There are similarities though in those 2 languages and some pieces of AS2 code can sometimes work as is in AS3, other pieces of code can sometimes be converted quite easily, and of course other pieces of code cannot at all be converted and a new AS3 code has to replace the old AS2 code.
All in all only a translation can be done between the two languages and this has to be done line by line. Evaluating if the line is compatible and if it's not replacing it with something that will work in AS3, etc ...
To add to the difficulty some AS2 projects can have code placed inside symbols all over the place. In that case a translation to AS3 will be very unlikely to achieve.

AS3 How to make an object shrink when it is not tapped

I have stumbled upon a problem that I do not seem to know how to solve. I have searched it up online but no answer has been found. It is that when you tap the Monster on the screen it grows, but when you don't tap it returns to its default size. I have managed to make it grow when tapped but I can not seem to make it so that when you release the tap it shrinks. An example of what I am talking about is on cookie clicker, that when you click the big cookie it grows but when you don't it goes to its original size. Here is my code so far:
var score:Number = 0;
var score_str:String;
var score_str_len:int;
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
Monster.addEventListener(TouchEvent.TOUCH_TAP, fl_TapHandler);
Monster.width = 363.5;
Monster.height = 344.9;
function fl_TapHandler(event:TouchEvent):void
{
Monster.width = 378.4;
Monster.height = 359;
score = score + 1;
score_str = score.toString();
score_str_len = score_str.length;
Taps_txt.text =
score_str_len > 3
? score_str.substr(0, score_str_len-3) + ',' + score_str.substr(score_str_len-3)
: score_str
;
}
Help will be greatly appreciated.
According to the actionscript reference, there are a lot of other touch events you can use, such as TouchEvent.TOUCH_OUT or TouchEvent.TOUCH_END.
I haven't used them so i can't tell you which one would be helpful to you but you can run some tests and see which of the event is fired when you remove your finger from the monster, just as you would do with mouse events.
So basically you would add another listener (best would be inside the fl_TapHandler function) for the release touchevent and put the monster variables back to their normal level.
If you want to have several monsters, the best way would be to create a class for it and call the functions that would be inside this class. Exemple:
Monster.addEventListener(TouchEvent.TOUCH_TAP, Monster.fl_TapHandler);
I hope this helps.
How about you store all monsters in an array?
var monsters:Vector.<Monster> = new Vector.<Monster>();
monsters.push(new Monster());
monsters.push(new Monster());
monsters.push(new Monster());
for(var monster : monsters) {
if(monster.isTapped){
monster.grow()
} else {
monster.shrink()
}
}

AS3, variables outside event handler function?

I have searched arond, but cant seem to find a proper answer to this. Lets say we have a super basic program which adds two numbers from 2 input text fields togehter and prints them out. Why cant i accses variables outside the event handler function? And what do i have to do in order to achieve this? The code is on a frame.
Why does this example not work? :
btn.addEventListener(MouseEvent.CLICK, cal);
var fnum:Number = Number(txt1.text);
var snum:Number = Number(txt2.text);
function cal(evt:MouseEvent){
txtOutput.text = String(fnum + snum);
}
And this example work?:
btn.addEventListener(MouseEvent.CLICK, cal);
function cal(evt:MouseEvent){
var fnum:Number = Number(txt1.text);
var snum:Number = Number(txt2.text);
txtOutput.text = String(fnum + snum);
}
I hate to be this guy, but I can't get it to replicate whats happening for you..
The only assumption that I can make is that the txt1.text & txt2.text aren't set yet when the button is clicked in example 1. Feel free to zip your project and dropbox it to me if you want me to investigate further :)

nonClickable buttons after loading same FLA 2nd time

Basically Im trying to make a FLA where if i click a button and close the FLA, next time I load the same FLA, the buttons that have been clicked before , now have an alpha of 0.5 and are nonClickable.
Currently i have only the logic for creating the buttons and saving the data:
private var savedData:SharedObject = SharedObject.getLocal("sharedStorage10");
private var buttonInfoArr:Array = [];
public function Main()
{
for (var i:int = 0; i < 4; i++)
{
var myImage_mc:MovieClip = new MovieClip();
myImage_mc.graphics.beginFill( 0xFFFFFF * Math.random() );
myImage_mc.graphics.drawRect(0,0,100,100);
myImage_mc.graphics.endFill();
myImage_mc.x = 50 + i * (myImage_mc.width + 10);
myImage_mc.y = 100;
myImage_mc.name = "myImage_mc" + i
this.addChild(myImage_mc);
myImage_mc.mouseEnabled = true;
myImage_mc.buttonMode = true;
myImage_mc.addEventListener(MouseEvent.CLICK, onClick);
}
}
private function onClick(ev:MouseEvent):void
{
var thisButton:MovieClip = ev.currentTarget as MovieClip
trace(thisButton.name)
thisButton.alpha = 0.5
thisButton.buttonMode = false;
thisButton.removeEventListener(MouseEvent.CLICK, onClick);
buttonInfoArr.push( thisButton.name );
savedData.data.myArray = buttonInfoArr;
}
But i dont know how to load/use the data and make the buttons (that have been clicked before) start with an alpha of 0.5 and without an EventListener(MouseEvent.CLICK, onClick)
PS:Could it be done without the use of Dictionary, cause i haven`t grasp my mind over it yet.
_EDIT__
to represent my problem in a more detailed way:
So im creating a game in which I collect objects that represent money.After I die in the game i get into the Shop Class in which i can buy different items depending on the money i have.
So lets say that after dying i have enough money to get item1 and item2. So i Click on button1 (representing item1) and button2 (representing item2) (when clicked their alpha gets to 0.5) and start the game again.Ok so now i have 1 life more and i get more money.But suddenly out of nowhere i feel something strange deep in me, something that i have never felt before -> HUNGER,
I shut down the PC (cause I love Earth )and run as fast as possible to the nearest shop.
lets say item1 is : +1life and item2 is : get more money from objects
After I had satisfied my primal needs I feel the strong urge to play that awesome game of mine again. But when i start the game I start to see some strange things: It seems that I havent got one life more and neither do i get more money and just to be sure that Im not hallucinating (or that i didnt rememer the lifes i had last time) I die on purpose so the Shop menu shows. So now what I see is making me sad -> button1 and button2 are active and have an alpha of 1( and that means that the game thinks that they were never bought/clicked, that i have never played this game before in my life)
So i go crazy (cause all that play before was for nothing and i have to start all over again).
After i calm down, I decide to change my life. 1st BIG change is adding some code into the game. To be more specific: a method that will save the items that i bougth, so when i close the game and start again the things i see will bring joy to my life - >: I still have the items that i bougth last time.
So long story short:
how do i make that when i click a button (it changes its alpha to 0.5)and close the FLA ,
whenever i open the same FLA, the button that i clicked before will be shown as clicked now(it alpha will be 0.5)
What is happening is normal. All variables are stored in RAM, which is temporary memory. Your button states are variables, too. Whenever any program closes, everything it has in RAM is deleted, thus the variables are cleared. This is true of all programs, in all programming languages. There are absolutely no exceptions.
Quite simply, you need to create a file that can store all this data long-term, on the hard drive (which is "permanent" memory).
If you're creating a game for the internet, you're going to need to store this data using SharedObjects (a.k.a. cookies).
If you're creating a game to be installed on the computer or mobile, you'll want to store data in a file. I recommend XML files, as they're the easiest to work with by far.
I am not going into either here, because those are lectures, not SO answers. See the above links to get started.
I didn't understand what exactly you're trying to do. But here is my simply approach would be that way.
// this is basically saved your latest clip we will control this if user click this button then we will be blocked
private var lastClickedButton:MovieClip;
private function onClick(ev:MouseEvent):void
{
var thisButton:MovieClip = ev.currentTarget as MovieClip
trace(thisButton.name)
thisButton.alpha = 0.5
thisButton.buttonMode = false;
thisButton.removeEventListener(MouseEvent.CLICK, onClick);
buttonInfoArr.push( thisButton.name );
savedData.data.myArray = buttonInfoArr;
// set the lastclicked button
lastClickedButton = thisButton;
// save this savedData sharedObject
savedData.data.lastClickedItem = lastClickedButton;
}
addEventListener(Event.ENTER_FRAME, loop)
{
if(savedData.data.lastClickedItem)
{
// do something here.
lastClickedButton.alpha = 0.5;
lastClickedButton.mouseEnabled=false;
// clear your sharedobject
savedData.flush();
}
}
I hope this will help you. good luck...

How to control sound/music playblack in AS3

I'm making a game with AS3. I would like to put a different music in each background.
So far, I've succeed to start the music when the player enters in a scene. But if he goes out, the music is still playing...(I want the music to stop when the players leave a scene).
I don't understand why the music is still playing.. Here's my code :
public function newBackground(thisBack:String):void{
var room = back.currentBack;
switch (thisBack){
case "maisonExt":
var mySound:Sound = new exterieur ();
mySound.play ();
My mp3 file is named "exterieur.mp3".
If you can think of anything it would be a great help.
Thanks !
You need to stop the music again. If you don't stop it, it will keep on playing :)
When you call .play it will return a SoundChannel which lets you control the playback -- changing position, stopping the playback, and accessing the SoundTransform which lets you control the volume. Here is some example code for you:
public var currentSoundChannel:SoundChannel;
public function newBackground(thisBack:String):void
{
var room = back.currentBack;
if (currentSoundChannel != null)
{
currentSoundChannel.stop()
}
var nextSong:Sound;
switch (thisBack){
case "maisonExt":
nextSong = new exterieur ();
break;
}
currentSoundChannel = nextSong.play();
}
If you want to learn more about sound, I would recommend this tutorial http://gamedev.michaeljameswilliams.com/2009/03/03/avoider-game-tutorial-9/. It's part of a larger tutorial which teaches you many aspects of game making, taking you from novice to capable of creating a game. You should really check that out.