How to remove if statement with an if statement? as3 - actionscript-3

I'm making a game where the character has to pass through the gate after getting the key
so when he gets the key the gate is suppose to go away, i used
if(character.hitTestObject(gate))
{character.visible = false;
youLose_text.visible = true; }
BUT when he gets the key: it's
if(character.hitTestObject(key))
{
gate.visible = false; }
NOW when i pass through the gate, i obviously get killed again
now how do i remove the previous if function through the next if function?

Just set/check a boolean. One already exists in gate.visible.
// If gate is visible and character hitTests gate then..
if(gate.visible && character.hitTestObject(gate)) {
character.visible = false;
youLose_text.visible = true;
}

If the gate is supposed to actually "go away", you should completely remove it, not just make it invisible... e.g.:
if(character.hitTestObject(key)) {
//don't forget to remove event listeners on the gate as well, if it had any
removeChild(gate);
gate = null;
} else if(gate != null) {
if(character.hitTestObject(gate)) {
character.visible = false;
youLose_text.visible = true;
}
}

Related

Suppressing a beep winform / textbox

I have a key sequence "CTRL+U" and in the form I have a KeyDown method that looks at the key variable e and does what it wants. It does not set a result.
It works.
But if the focus on the form is in a text box and I press the key combination, it still works, but then I get the beep.
I am a little confused as to how to resolve this as it sounds a lot of work to have to suppress a key event in every control (should I have several text boxes).
The handler:
private void XXXXXForm_KeyDown(object sender, KeyEventArgs e)
{
if(e.Control)
{
bool bHandle = false;
bool bChecked = true;
if (e.KeyCode == Keys.U)
{
bChecked = false;
bHandle = true;
}
else if (e.KeyCode == Keys.T)
{
bChecked = true;
bHandle = true;
}
if(bHandle)
{
// Do stuff
}
}
}
I found this information on another website:
The "e.Handled = true;" statement is not doing what you think here.
The documentation [^]for this is confusing, and one could interpret it
the way you have. However, you need to realize that they are talking
about setting "Handled" in the KeyPress Event. To make matters worse,
the KeyPressed event uses KeyPressEventArgs not KeyEventArgs.
Instead use e.SuppressKeyPress = true;
That was the solution!

Actionscript 3: How to detect when a button isn't pressed in time?

Okay, this is another question for the game I am making. I am putting together a second level, where you need to press a long series of keys at the right time. Pressing them too early or too late results in failure. What I need to know is how to detect when a key ISN'T pressed, when it should have been. This is what I have so far:
var count3:Number = 23;
var myTimer3:Timer = new Timer(1000, count3);
var timeLeft3:Number = count3;
var buttonPressed:Boolean = false;
var btnCounter:Number = 2;
var btnTimer:Timer = new Timer(1000, btnCounter);
myTimer3.addEventListener(TimerEvent.TIMER, countdown3);
myTimer3.start();
btnTimer.addEventListener(TimerEvent.TIMER, btnCountdown);
btn1.addEventListener(MouseEvent.CLICK, buttonPress);
btn1.visible = false;
btn2.visible = false;
btn3.visible = false;
btn4.visible = false;
btn5.visible = false;
btn6.visible = false;
btn7.visible = false;
btn8.visible = false;
btn9.visible = false;
function countdown3(event:TimerEvent): void {
if (((count3)-myTimer3.currentCount)==20) {
btn1.visible = true;
btnTimer.start();
} else if (((count3)-myTimer3.currentCount)==19) {
btn1.visible = true;
} else {
btn1.visible = false;
}
}
function btnCountdown(event:TimerEvent):void {
if (((btnCounter)-btnTimer.currentCount)==0) {
if (buttonPressed = true) {
btnTimer.stop();
} else {
gotoAndStop(2);
}
}
}
function buttonPress (event:MouseEvent): void {
buttonPressed = true;
}
For some reason, it won't do anything when btnCounter hits 0. If someone could help me sort this out, that would be awesome. Thanks.
N.B. This is a personal project, I am just learning actionscript
Start a timer for the time duration when the button is supposed to be pressed (for example, if the button must be pressed within 2 seconds, set the timer's interval to 2 seconds).
Then create a global Boolean variable (or class member variable, however your scene is set up) and set it to false initially. When the button is pressed, set this variable to true and disable the timer.
If the timer fires and this variable is false, it means that it hasn't been disabled by the button so the user didn't click the button within 2 seconds.
Don't use terribly short times - timers are not very accurate and processor speed may have an effect on their duration. (Human reaction times aside.)

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);

else statement always run even the answer is true

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.

How to make a movie clip invisible depending on a value in another layer?

I am trying to make an "achievements" page for my game, where if the user has a high score greater than 100, then the achievement can be unlocked. I tried using a code like this, but it didn't seem to work!
function Check();
if(endscreen_mc.highscore_txt > 100)
{
medals.roachLock.visible = false;
}
else if(endscreen_mc.highscore_txt < 100)
{
medals.roachLock.visible = true;
}
else if(endscreen_mc.visible == 100)
{
medals.roachLock.visible = true;
}
The high score value is saved inside a shared object. What's wrong with my code, and what can I do to fix it?
What is the type of the endscreen_mc.highscore_txt property? If it is a TextField, you'll first need to access the text property of the textfield and cast it to a Number like so:
var score:Number = Number(endscreen_mc.highscore_txt.text); // this casts the String value of the text property to a Number
if(score > 100)
{
medals.roachLock.visible = false;
}
else
{
medals.roachLock.visible = true;
}
Additionally I've simplified the if/else statement. Now if the score is not greater than 100, the lock will be visible.
visible property in ActionScript is type Boolean - meaning that it can have only values "true" or "false", so your third statement
else if(endscreen_mc.visible == 100)
will not work. Mabye you wanted to put there
endscreen_mc.highscore_txt
Also, one important thing, your function Check(); doesn't have a body, meaning that each actionscript function looks like this:
function functionName(){}//your if statement goes between curly brackets.
So if I'm getting this right this is what your function should look like:
function Check(){
if(endscreen_mc.highscore_txt > 100){
medals.roachLock.visible = false;
} else {
medals.roachLock.visible = true;
}
}