Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

New to ActionScript 3.0 - Got some questions with IF statements!

New Here ,
Jan 21, 2014 Jan 21, 2014

Let's say I'm creating a game where you can edit character skills.

For now, there is only the "endurance" valuable, which is 10. You can decrease it, but I want it to stop at 5 (as a hero with 0 or negative endurance would be meaningless).

I have two questions, but let's look at the code first:

//Defaults var hero_endurance:int = 10;

var string_hero_endurance:String = String(hero_endurance);

box_hero_endurance.text = string_hero_endurance;

//Endurance Controls function hero_endurance_decrease(MouseEvent😞void{

hero_endurance = hero_endurance-1;

var string_hero_endurance:String = String(hero_endurance);

box_hero_endurance.text = string_hero_endurance;

}

if (hero_endurance > 5){

button_hero_endurance_down.addEventListener(MouseEvent.CLICK, hero_endurance_decrease);

}

Now my first question:

This code doesn't work. The valuable keeps going down (below 5), like the if statement isn't there at all. How can I fix this?

Second question:

Maybe you have noticed, the function always refreshes the valuable in the text box by:

var string_hero_endurance:String = String(hero_endurance);

box_hero_endurance.text = string_hero_endurance;

Actionscript 2.0 didn't even require such a code, any change you did to a valuable would reflect in a text box. Is there a simple way to do this, or do I have to use this code for each and every case?

Thanks on advance!

TOPICS
ActionScript
408
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 21, 2014 Jan 21, 2014

The if statement is not going to do anything more than keep adding the same listener to the button - once is enough.  If you do not want the value to fall below 5 then put the continitonal around the line that decreases it instead.

You are creating a variable unnecessarily.  You can assign to the textfield using the integer....

        box_hero_endurance.text = String(hero_endurance);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 21, 2014 Jan 21, 2014

Try this code:

import flash.events.MouseEvent;

//Defaults

if (!this.isInit)

{

          var _hero_endurance:int = 0;

          init();

          var isInit:Boolean = true;

}

function init():void

{

          hero_endurance = 10;

          button_hero_endurance_down.addEventListener(MouseEvent.CLICK, hero_endurance_decrease);

}

//Endurance Controls

function hero_endurance_decrease(e:MouseEvent):void

{

          hero_endurance--;

}

function get hero_endurance():int

{

          return _hero_endurance;

}

function set hero_endurance(value:int):void

{

          _hero_endurance = value;

          if (_hero_endurance < 6)

                    button_hero_endurance_down.removeEventListener(MouseEvent.CLICK, hero_endurance_decrease);

          box_hero_endurance.text = String(_hero_endurance);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 22, 2014 Jan 22, 2014
LATEST

Thanks, I'll give it a try.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines