dcsimg
A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: HELP - Platform game

  1. #1
    Senior Member FohpreF1's Avatar
    Join Date
    Apr 2006
    Location
    Australia
    Posts
    100

    HELP - Platform game

    Hey, I am making a platform game and I have set up the basics (character movement, solid walls and platforms) but I am having trouble with collecting items and adding to your score.

    I have the following code:

    onClipEvent (enterFrame) {
    if (this.hitTest(_root.Player)) {
    _root.score += 1;
    }
    }

    I have this code in the actions of the object, and all the instance names are correct, can anybody help me?

  2. #2
    Senior Member Gloo pot's Avatar
    Join Date
    Aug 2005
    Location
    Australia Mate!
    Posts
    874
    what are you trying to do...
    When "this" (Being the MC you have the code on) hits the mc with the instance "Player" (Take note of capital ect, instances are case sensative.) score will go up by 1 every frame...

    That will work perfectly for that... if it doesnt you have made a mistake with instance names.

    I would sugest adding a black frame to what your collecting and send that item to it so that you cant collect it over and over again...
    92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!

  3. #3
    Senior Member FohpreF1's Avatar
    Join Date
    Apr 2006
    Location
    Australia
    Posts
    100
    "score will go up by 1 every frame..."

    Does that mean my score object will go to the next frame?
    Because its a dynamic text box and I want the number in it to go up by one.

    Also, will it matter that my hero mc has other mcs in it?
    Last edited by FohpreF1; 07-07-2006 at 11:42 PM. Reason: Forgot something

  4. #4
    Senior Member Gloo pot's Avatar
    Join Date
    Aug 2005
    Location
    Australia Mate!
    Posts
    874
    No... Your code is exectured once every frame, so if you hit the thing your collecting your varible score will go up by one... the next frame it will run again and if your still there it will go up again and continue to go up till you have moved or the object moves... It does not send you dynamic text box to another frame...
    92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!

  5. #5
    file not found Captain_404's Avatar
    Join Date
    Apr 2006
    Posts
    457
    with your current code, it would be that whenever the character touches the items, he gains points

    now, if the character doesn't needs to use that item later and only gains points from it, you can use the hitTest on the item to tell itself to play so you're first frame would be the item, then the second would have

    _root.score += 1;
    stop();

    and be empty.

    I can't help you if your character needs to use that item later...

  6. #6
    Senior Member FohpreF1's Avatar
    Join Date
    Apr 2006
    Location
    Australia
    Posts
    100
    I tried captains idea and I got the object to go blank after being touched, but the score still isn't working.
    Would anyone be able to check the .fla file and see if you can see the problem?
    Attached Files Attached Files

  7. #7
    Feeling adventurous? T1ger's Avatar
    Join Date
    Mar 2004
    Posts
    850
    Here are the problems:

    The textbox:
    Its instance name is 'score'. Then you can access its value with 'score.text'. Better would be to delete instance name, and use a property called 'var'. Enter 'score' in there. Now it will always show the value of the score variable.

    The score variable:
    Hasnt been declared, so flash thinks its a string. In the hero mc, where you set gravity etc, write '_root.score = 0;'.

    And let the coin have two keyframes, the second should have no graphics and no script. The first should contain the graphics and a stop(). When you do the hitTest, do this script if it returns true:

    _root.score += 1;
    gotoAndStop(2);

    This way we have less script inside movieclips, and its easier to debug and use.

    And a tip for future gamedevelopment: Try to write all actionscript on the _root timeline. That means you deselect everything and write script there. That way its much simpler to debug, and let others debug your code. And its cleaner.

    I don't have a photograph, but you can have my footprints. They're upstairs in my socks.

  8. #8
    Member
    Join Date
    May 2006
    Location
    Michigan
    Posts
    99
    Create a Action on the main stage

    score = 0;

    then on the score box var property type

    _root.score


    thne under the hit test ...

    _root.score = +1;


    later mate
    Jay

    The man with the plan !!!

  9. #9
    Member
    Join Date
    Jun 2006
    Posts
    43
    I agree you should put it like this:
    onClipEvent (enterFrame) {
    if (this.hitTest(_root.hero)) {
    _root.score.text += 1;
    gotoAndPlay (2);
    }
    }

  10. #10
    Senior Member FohpreF1's Avatar
    Join Date
    Apr 2006
    Location
    Australia
    Posts
    100
    I have been trying this, but +=1 puts a 1 on the end of the score eg. 01 instead of going to 1, but it doesn't matter, I made the score start at 5 and you take away one when you get a coin (-=1 seems to work properly??).

    Anyway, it will do for the time being.

    Thanks everyone for your help!

  11. #11
    Back to FK Boards!
    Join Date
    Apr 2006
    Location
    Troy
    Posts
    234
    here's an easy way:

    1. create a dynamic text box and do not type ANYTHING in it.
    2. click the dynamic text box, go down to where it says "var" and in that space type: score
    3. click the frame and open your actions panel.
    4. in the actions panel type score = 0
    5. click the coin and open it's actions panel.
    6. put this code on the coin on the first line:

    Code:
    onClipEvent (enterFrame) {
        if (this.hitTest(_root.hero)) {
            _root.score += 1;
            }
        }
    you're also going to have to double click the coin and on the second frame of the coin you're going to have to take out all the actionscript except stop();
    Last edited by Dark Cloud; 07-12-2006 at 09:46 AM.
    Email: Jacktown012@aim.com
    AIM: jacktown012
    MSN: megafire987@hotmail.com

  12. #12
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,928
    When using dynamic text boxes no one should be using var to set the data inside them anymore, they should be given an instance name and the .text ( or .htmlText ) properties should be used.

    _root.score += 1;

    Would be better written as

    _root.score ++;

    As touched on above, instance names should be used, so as stated earlier, score=0; should be set in the _root of the movie ( As a variable ), so the text box instance name could be called scoreDisplay.

    As to the code on the mc, for sake of simplicity having an enterFrame on it, if you're using F7 or above try this code instead.
    Code:
    onClipEvent(load){
    	var collisionFlipFlop=0;
    	this.onEnterFrame=function(){
    		if(++collisionFlipFlop==2){
    			collisionFlipFlop=0;
    			if(this.hitTest(_root.Player)) {  //or hero ?
    				_root.score++;
    				_root.scoreDisplay.text=_root.score;
    				this.swapDepths(_root.getNextHighestDepth());
    				this.removeMovieClip();
    			}
    		}
    	}
    }
    The "collisionFlipFlop" is because you don't need to check for hitTest every frame, it's just a waste of time, so we just test for it every other frame ( Alter the 2 to reduce the number of tests even further ).

    The variable score is increased if we get a collision, and we then make the dynamic text box instance ( "scoreDisplay" ) equal to the score.

    The next two lines are just a hacky way of removing the movieclip.

    Can't believe the amount of poor advice in this thread though. People still using F5 techniques when F9 is out in beta.

    Squize.

  13. #13
    Back to FK Boards!
    Join Date
    Apr 2006
    Location
    Troy
    Posts
    234
    Quote Originally Posted by Squize
    When using dynamic text boxes no one should be using var to set the data inside them anymore, they should be given an instance name and the .text ( or .htmlText ) properties should be used.

    _root.score += 1;

    Would be better written as

    _root.score ++;

    As touched on above, instance names should be used, so as stated earlier, score=0; should be set in the _root of the movie ( As a variable ), so the text box instance name could be called scoreDisplay.

    As to the code on the mc, for sake of simplicity having an enterFrame on it, if you're using F7 or above try this code instead.
    Code:
    onClipEvent(load){
    	var collisionFlipFlop=0;
    	this.onEnterFrame=function(){
    		if(++collisionFlipFlop==2){
    			collisionFlipFlop=0;
    			if(this.hitTest(_root.Player)) {  //or hero ?
    				_root.score++;
    				_root.scoreDisplay.text=_root.score;
    				this.swapDepths(_root.getNextHighestDepth());
    				this.removeMovieClip();
    			}
    		}
    	}
    }
    The "collisionFlipFlop" is because you don't need to check for hitTest every frame, it's just a waste of time, so we just test for it every other frame ( Alter the 2 to reduce the number of tests even further ).

    The variable score is increased if we get a collision, and we then make the dynamic text box instance ( "scoreDisplay" ) equal to the score.

    The next two lines are just a hacky way of removing the movieclip.

    Can't believe the amount of poor advice in this thread though. People still using F5 techniques when F9 is out in beta.

    Squize.
    my way still works though
    Email: Jacktown012@aim.com
    AIM: jacktown012
    MSN: megafire987@hotmail.com

  14. #14
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    For the score text problem I suggest the exact same way Squize has described. It is best to keep score in separate variable and only use text box to show it when needed. This helps to avoid several problems, for example string vs number conversions.

  15. #15
    Member
    Join Date
    Jun 2006
    Posts
    43
    It goese 01 because you are doing string add. You needs integer add.
    declar it first dude.

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