else statement always run even the answer is true - actionscript-3

tnx! it works. but when i tried to add another object named, p1_2 and add the "trace" thing to the code,
it goes back to the same problem.
p1_1.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void
{
question.text = "shape?";
}
submit.addEventListener(MouseEvent.CLICK, onClickss);
function onClickss(e:MouseEvent):void
{
trace("ans.text = "+ans.text);
if (ans.text == "circle")
{
p1_1.visible = false;
}
else
{
gotoAndStop(6);
}
}
p1_2.addEventListener(MouseEvent.CLICK, onClick2);
function onClick2(e:MouseEvent):void
{
question.text = "Color?";
}
submit.addEventListener(MouseEvent.CLICK, onClickss2);
function onClickss2(e:MouseEvent):void
{
trace("ans.text = "+ans.text);
if (ans.text == "red")
{
p1_2.visible = false;
}
else
{
gotoAndStop(6);
}
}
what should i do.? do i nid to seperate p1_2 in another frame
and make another inputtextfield for it?
im planning to add 5 objects on the stage. until p1_5. -_-

Before the line...
if(ans.text == "circle") {
...add:
trace("ans.text = "+ans.text);
Clicking the submit button will trace the actual value of ans.text to the output panel when you test your movie in Flash. If you get a null reference error then ans has not been instantiated. Otherwise, knowing the actual value of ans.text should point you in the right direction to solve the problem.

Related

How can I capture if the user clicked a wrong button?

I made a game remember for the first fishes. I found a code like this. I also have wrong buttons.
Wrong buttons list:
pinkButton.addEventListener(MouseEvent.CLICK, pinkClick);
whiteButton.addEventListener(MouseEvent.CLICK, whiteClick);
greyButton.addEventListener(MouseEvent.CLICK, greyClick);
And I use this code
import flash.events.MouseEvent;
var checkString:String = "";
 
//Create event listeners and their functions.
YellowButton.addEventListener(MouseEvent.CLICK, yellowClick);
RedButton.addEventListener(MouseEvent.CLICK, redClick);
BlueButton.addEventListener(MouseEvent.CLICK, blueClick);
/*True choices*/
function yellowClick(evt:Event):void
{
//In each event listener function, add a letter or
//string to the checkString variable.
checkString += "y";
//Then, see if the string matches or not.
check();
}
function redClick(evt:Event):void
{
checkString += "r";
check();
}
function blueClick(evt:Event):void
{
checkString += "b";
check();
}
/*True choices*/
 
//If the proper sequence is red, yellow, blue, the string would read "ryb".
function check():void
{
if(checkString == "ryb")
{
//Clear the checkString for convenience before going on.
clearString();
//CODE TO GO TO NEW FRAME
gotoAndStop(3);
}
else
{
//Make sure the string is at least 3 characters long.
if(checkString.length >= 3)
{
clearString();
gotoAndStop(1);
}
}
}
function clearString():void
{
//You will want to have a function for clearing the string.
//This is especially useful if you have a button for "start over."
checkString = "";
}
 
if I click yellow, red, blue it works. How can I make wrong choices? Do I have to write a code for all possibilities? Player has 1 chance. For example if player clicked 2 false and 1 true button,or 2 true and 1 false, this results in a loss for the player.
Use an array of values. Like
var correctSequence:Array = ["r", "y", "b"];
then have a incrementing variable to give you control over traversing the array
var arrayPosition:int = 0;
and you need a variable to hold the Boolean value of whether or not there have been any wrong guesses:
var noneWrong:Boolean = true;
Then you could do something like
private function playerNextGuess(e:MouseEvent):void{
if (e.target._color == correctSequence[arrayPosition] && noneWrong == true){
arrayPosition++;
if (arrayPosition == 3){
playerWin();
arrayPosition = 0;
}
} else {
// put whatever logic you want for when the guess is wrong
noneWrong = false;
arrayPosition++;
if (arrayPosition == 3){
playerLose();
arrayPosition = 0;
}
}
This will make it so that 3 guesses are made before the results (right or wrong) are given to the player. But it won't tell the player which were right and which were wrong. If none are wrong, the win function is called. If any are wrong the lose function is called. Is that what you wanted?
Hopefully that gets you going in the right direction. Let me know if anything I wrote isn't abundantly clear.

Going from one scene to next and gettingType1009 Null object reference errors

Okay so in my main menu for my game, I have a glow effect so that when you hover over the invisible button 'startdesu', the script will make the whole menu glow, fading in and out. This code (from the event listener onwards) has been repeated for each button on the menu, with appropriate changes to the button instance name and the function names. The only problem is, when I click 'startdesu' and it takes me to the next scene, I start getting repetitive errors, "TypeError: Error #1009: Cannot access a property or method of a null object reference.
at bjvb_fla::MainTimeline/increaseGlow()". I've tried removing the glow event listeners when the buttons are clicked and everything but no luck. Please help! ;0;
Here is the essential code for brevity's sake, but I can post the whole thing if it helps.
(also, i get the same Null error for something else when i go from the game back to the start menu.
import flash.filters.*;
import flash.events.Event;
stop();
wow.alpha=0.5;
var menuGlow:GlowFilter = new GlowFilter();
menuGlow.color = 0xffffff;
menuGlow.blurX = 20;
menuGlow.blurY = 20;
menuGlow.alpha = 0;
menu.filters = [menuGlow];
var glow:Boolean = false;
startdesu.addEventListener(MouseEvent.MOUSE_OVER, addGlow);
startdesu.addEventListener(MouseEvent.MOUSE_OUT, removeGlow);
startdesu.addEventListener(Event.ENTER_FRAME, increaseGlow);
function addGlow(e:MouseEvent):void
{
glow = true;
}
function removeGlow(e:MouseEvent):void
{
glow = false;
}
function increaseGlow(e:Event):void
{
if(glow == true)
{
menuGlow.alpha = menuGlow.alpha + 0.02;
}
else if(glow == false)
{
menuGlow.alpha = menuGlow.alpha - 0.02;
}
menu.filters = [menuGlow];
}
This is the line that is likely causing the error:
menu.filters = [menuGlow];
There is probably no object with the instance name 'menu' in your second scene. You could fix the error by just adding a check if the object exists, like so:
function increaseGlow(e:Event):void
{
//if there's no menu object, return
if(!menu) return;
if(glow == true) {
menuGlow.alpha = menuGlow.alpha + 0.02;
} else {
menuGlow.alpha = menuGlow.alpha - 0.02;
}
menu.filters = [menuGlow];
}
But the correct solution would be to remove the event listeners. Not sure why it didn't work for you, but you should be able to just add this in the click event handler before you switch your scene.
startdesu.removeEventListener(Event.ENTER_FRAME, increaseGlow);
What errors do you get when you try to remove the event listener?

how to disable movieclip?

I am using Adobe Flash CS5, Action Script 3.0. I want to disable my movie clip. How to do that?
My code:
var index:Array = ([0,1,2,3]);
var radioGroup1:RadioButtonGroup = new RadioButtonGroup("question1");
rb1.group = radioGroup1;
rb2.group = radioGroup1;
rb3.group = radioGroup1;
rb4.group = radioGroup1;
b1.addEventListener(MouseEvent.CLICK, submitclick,false);
function submitclick(event:MouseEvent):void
{
if (radioGroup1.selectedData == null)
{
b1.mouseEnabled = false;
//ExternalInterface.call("Test","Please selecte one answer");
//return;
} else {
if (radioGroup1.getRadioButtonIndex(radioGroup1.selection) == 2)
{
myscore += 10;
}
}
if (compquest>=maxquest)
{
gotoAndStop(1,"Scene 6");
}
else
{
MovieClip(this.root).gotoAndStop(1, "Scene " + questions[compquest++]);
}
}
I want to disable this b1 movieclip. So anyone could tell me what am I doing wrong?
If, by disable, you mean to make it unclickable and faded like a disabled button. Just reduce the alpha a bit and set its mouseEnabled property to false.
function submitClick(event:MouseEvent){
b1.alpha = .6;
b1.mouseEnabled = false;
}
This way the user will not be able to click it and it appear disabled.
First you could write, because event.target is p1:
function submitclick(event:MouseEvent):void
{
event.target.mouseEnabled = false;
}
On the other hand, the condition you want to evaluate in your if statement is probably false:
radioGroup1.selectedData == null
Therefore the associated code isn't executed:
b1.mouseEnabled = false;
You should test in your function and before your if statement:
trace(radioGroup1.selectedData == null);

AS3: How do I play a sound when the currentframe ==?

My code below unfortunately results in the sound repeating at the framerate of the movie. I just need a simple "play a sound at a specific frame" function but can't figure this one out. Thanks for your help.
addEventListener(Event.ENTER_FRAME,soundFunction);
function soundFunction(event:Event)
{
if (naturepage.ocean_btns.currentFrame == 2)
{
ocean_channel.stop();
ocean_channel = ocean1.play(0,int.MAX_VALUE);
mySO.data.ocean = "2";
mySO.flush();
}
}
This seems about right. Are you sure the naturepage.ocean_btns movieclip is not stopped at the second frame or looping very few frames over and over?
Another option is to use a flag to see if you already played the sound:
var alreadyPlayedSound:Boolean = false;
addEventListener(Event.ENTER_FRAME,soundFunction);
function soundFunction(event:Event)
{
if (naturepage.ocean_btns.currentFrame == 2 && !alreadyPlayedSound)
{
alreadyPlayedSound = true;
ocean_channel.stop();
ocean_channel = ocean1.play(0,int.MAX_VALUE);
mySO.data.ocean = "2";
mySO.flush();
}
}
Or to remove the event listener once you played the sound:
addEventListener(Event.ENTER_FRAME,soundFunction);
function soundFunction(event:Event)
{
if (naturepage.ocean_btns.currentFrame == 2)
{
ocean_channel.stop();
ocean_channel = ocean1.play(0,int.MAX_VALUE);
mySO.data.ocean = "2";
mySO.flush();
removeEventListener(Event.ENTER_FRAME,soundFunction);
}
}

Change Variable on Mouse Event

I'm trying to change the variable that's set to 0 into 1 on a mouse event.
I have 3 movie clips 'maskedbgmc' and when clicked its supposed to change a variable.
But it doesn't change the variable as far as i can see. What do i miss?
Thanks for your time :)
var checkCard1:Number = 0;
maskedbg_mc.addEventListener(MouseEvent.MOUSE_DOWN, cardChecked1);
function cardChecked1 (event:MouseEvent):void {
checkCard1 = 1;
}
var checkCard2:Number = 0;
maskedbg_mc2.addEventListener(MouseEvent.MOUSE_DOWN, cardChecked2);
function cardChecked2 (event:MouseEvent):void {
checkCard2 = 1;
}
var checkCard3:Number = 0;
maskedbg_mc3.addEventListener(MouseEvent.MOUSE_DOWN, cardChecked3);
function cardChecked3 (event:MouseEvent):void {
checkCard3 = 1;
}
if(checkCard1 == 1) {
trace('Nice!');
}
else if(checkCard2 == 1) {
trace('Better!');
}
else if(checkCard3 == 1) {
trace('King!');
}
Note that events happen asynchronously. This means that your if statements will execute the moment this code is interpreted, but the variables will only be modified when the mouse events occur. If you trace your variables value inside your event handler functions, you will likely see that it does indeed change when the mouse button is pressed.
Maybe what you want to do is add your if statements into a function, like so:
function checkCards() : void {
if(checkCard1 == 1) {
trace('Nice!');
}
else if(checkCard2 == 1) {
trace('Better!');
}
else if(checkCard3 == 1) {
trace('King!');
}
}
You can then invoke this method inside your event listeners, and it will check the card variables using the above logic. An example of it used inside the cardChecked3() method:
maskedbg_mc3.addEventListener(MouseEvent.MOUSE_DOWN, cardChecked3);
function cardChecked3 (event:MouseEvent):void {
checkCard3 = 1;
checkCards();
}
Hope this helps.
Does the event have to be a MouseEvent.MOUSE_DOWN? If you are only interested in clicks I would try changing that to a MouseEvent.CLICK.
Just a thought tho.
Try changing your code to this for the last part
function cardChecked3 (event:MouseEvent):void {
checkCard3 = 1;
if(checkCard1 == 1) {
trace('Nice!');
}
else if(checkCard2 == 1) {
trace('Better!');
}
else if(checkCard3 == 1) {
trace('King!');
}
}