A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: is there something wrong with my code?

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    71

    is there something wrong with my code?

    this is my code for a sleep button in my game:
    Code:
    on(release){ 
    if(day >= 1){
    day += 1;
    hp += 50;
    }
    else if(day = 25){
    _root.gotoAndPlay ("cat")
    }
     else if(day=30){ 
    this.enabled = false; 
    }
    }
    when I test my game, day doesn't stop at 30 or go to "cat" at 25.....
    _______________________________

    also how can I change this code:
    Code:
    on(release){
    	if(exp == 0){
     hp -= 10;
    gotoAndPlay("talk 1");  
    }else if(hp<10) {
    gotoAndPlay ("no hp left");
    }else if(exp == 10) {
    hp -= 10;
    gotoAndPlay ("talk 2");
    }else if(hp<10){
    gotoAndPlay ("no hp left");
    }
    }
    to make it so that when you go back to the chat button and you have 0 exp, instead of going back to "talk 1" you go to "talk 2"?

    i know, this is alot to ask, im just stumped when it comes to these codes T_T

  2. #2
    Senior Member Steven FN's Avatar
    Join Date
    Mar 2010
    Location
    CA, USA
    Posts
    276
    Ok not sure where you learned about IF and ELSE statements but for basic use:

    Code:
    if(a == b){
       // a = b, equals, true
    }else{
       // a != b, does not equal, false
    }
    I am not one to use ELSE IF statements because they seem to get confusing when you have too many. You can always use TRACE to test if your function works. But try the following in your code and it should work, just remove ELSE from your ELSE IF's:

    Code:
    on(release){ 
       if(day >= 1){
          day += 1;
          hp += 50;
       }
       if(day == 25){
          _root.gotoAndPlay ("cat")
       }
       if(day == 30){ 
          this.enabled = false; 
       }
    }
    Almost using one equal (=) is used for setting variables to values while (==) is used for comparing equality between 2 variables. Usually if you have only 1 (=) and your function ran through to it, it would return true every time regardless of actually meeting the conditions.

    Also make sure you are reaching the "day" variable by adding "trace(day);" at the top of your code. You may be trying to reach "_root.day" if you set the variable in the main timeline.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    71
    it didn't work, my sleep button still went past 30 and didnt stop at 25 :/

  4. #4
    Senior Member Steven FN's Avatar
    Join Date
    Mar 2010
    Location
    CA, USA
    Posts
    276
    Ok, can you upload a source file or stripped down source? I'm sure the code should work and might be another issue altogether.

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    71
    What Im trying to do is that when you hit the sleep button you gain +50 hp and +1 day, on day 25 a screen will popup and on day 30 another screen will popup: http://s000.tinyupload.com/?file_id=...75939763067534

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Firstly you need to embed your font or fonts.

    You don't need to have 4 versions of textfield var day and textfield var hp, just have them on one layer on their own perhaps.

    your button code is slightly mangled up too.

    try:
    Code:
    on (release) {
    	if (day < 25)
    	{
    		hp += 50;
    		day += 1;
    	}
    	else if (day == 25)
    	{
    		hp += 50;
    		day += 1;
    		_root.gotoAndPlay("cat");
    	}
    	else if (day == 30)
    	{
    		_root.gotoAndPlay("pickyourguy");
    	}
    }
    ini theory it will never get to day = 30 as you make it do things when it reaches 25, thats as far as your upload goes anyway.

    hope it helps

    my hobby
    .

  7. #7
    Member
    Join Date
    Aug 2012
    Posts
    55
    25 is lower than thirty so you're bound to get a clash.

    try this

    on (release) {
    if (_root.day<25){
    _root.hp += 50;
    _root.day += 1;
    }
    else if (_root.day>25 && _root.day<30) {
    _root.hp += 50;
    _root.day += 1;
    }
    else if (_root.day == 25) {
    gotoAndStop("cat");
    } else if (_root.day == 30) {
    gotoAndStop("pickyourguy");
    }
    }

    and distribute your cat and pickyourguy to different layers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center