Solved

as2 increment error

Posted on 2011-03-07
13
351 Views
Last Modified: 2012-05-11
hello
i am having trouble incrementing correct values in a score. i have 8 coin mc's on the stage with the following code and i am trying to add 5 to the score for each time the char hits the coin. but what is happening is that the first hit generates 5 then on the next hit instead of 10 it becomes 55 then 555 etc. where am i going wrong with this? thanks
STAGE VARS

score.text = 0;
// _root.score is set to 0
health.text = 100;
-----------------------------------------------------

COIN MC's

onClipEvent (enterFrame) {
	if (this.hitTest(_root.char)) {
		// if this hits the char (_root.char)
		_root.score.text+=5;
		// _root.score goes up 5
		unloadMovie(this);
		// this movie clip is unloaded
	}
}

Open in new window

0
Comment
Question by:peter_coop
  • 7
  • 6
13 Comments
 
LVL 17

Expert Comment

by:deepanjandas
Comment Utility
Try this:
 
onClipEvent (enterFrame) {
        if (this.hitTest(_root.char)) {
                // if this hits the char (_root.char)
                _root.score.text+=5;
                // _root.score goes up 5
                delete this.onEnterFrame;
                unloadMovie(this);
                // this movie clip is unloaded
        }
}

Open in new window


Warm Regards
Deepanjan Das
0
 

Author Comment

by:peter_coop
Comment Utility
hi still the same. the first hit == 05 the second hit == 055 etc.thanks
0
 
LVL 17

Expert Comment

by:deepanjandas
Comment Utility
Opps I am sorry.
Do this:
 
//stage vars:
STAGE VARS
var scoreInt:Number = 0;
score.text = scoreInt;
// _root.score is set to 0
health.text = 100;


onClipEvent (enterFrame) {
        if (this.hitTest(_root.char)) {
                // if this hits the char (_root.char)
                _root.scoreInt+=5;
                _root.score.text = _root.scoreInt;
                // _root.score goes up 5
                delete this.onEnterFrame;
                unloadMovie(this);
                // this movie clip is unloaded
        }
}

Open in new window


Warm Regards
Deepanjan Das
0
 

Author Comment

by:peter_coop
Comment Utility
hi. what is happening is that the score is being added on the first coin but not on the others. still stays at 5. i have placed the code in all my coin mc's. thanks
0
 
LVL 17

Expert Comment

by:deepanjandas
Comment Utility
Kindly share your fla for debug.

Warm Regards
Deepanjan Das
0
 

Author Comment

by:peter_coop
Comment Utility
here you go. bear in mind this is just a tutorial file i downloaded to get started. thanks
platformgame.fla
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 17

Expert Comment

by:deepanjandas
Comment Utility
Place this way:  
//stage vars:
scoreInt = 0;
score.text = scoreInt;
// _root.score is set to 0
health.text = 100;


onClipEvent (enterFrame) {
 
        if (this.hitTest(_root.char)) {
                // if this hits the char (_root.char)

                // _root.score goes up 5
                delete this.onEnterFrame;//this might not work as its AS1.0
                unloadMovie(this);
                // this movie clip is unloaded
        } else {
            _root.scoreInt += 5;
        }
         _root.score.text = _root.scoreInt;
}

Open in new window


Warm Regards
Deepanjan Das
0
 

Author Comment

by:peter_coop
Comment Utility
hi. that just creates a timer in the score. it just keeps incrementing. thanks
0
 
LVL 17

Expert Comment

by:deepanjandas
Comment Utility
I am lost on what you are looking forward to.
SO you want the score to increment on every hitTest and then stop.

Warm Regards
Deepanjan Das
0
 

Author Comment

by:peter_coop
Comment Utility
ok. let me try to explain further. as an example lets assume i have a mc on the stage which is the coin which as the following as attached. the code for the coin will increment the score by 1 on a hit. that works. what dosen't work is if i try to increment by any other value, say 5 or 2. so if coin 1 had an inc value of 5 then when hit the score becomes 5. if i place another coin on the stage and hit both it should be 10 but instead it is 55. it is not adding to the numebrs. the score is a dynamic text field with an instance name of score. thanks and i hope that is clearer.
COIN MC CODE

onClipEvent (enterFrame) {
	if (this.hitTest(_root.char)) {
		// if this hits the char (_root.char)
		_root.score.text++;
		// _root.score goes up 1
		unloadMovie(this);
		// this movie clip is unloaded
	}
}

Open in new window

0
 
LVL 17

Accepted Solution

by:
deepanjandas earned 250 total points
Comment Utility
This worked for me:

On the first frame:
_root.scoreInt = 0;
root.score.text = _root.scoreInt;

Open in new window


On the coin:
onClipEvent (enterFrame) {
        if (this.hitTest(_root.char)) {
                // if this hits the char (_root.char)
               _root.scoreInt += 5;
	       _root.score.text = _root.scoreInt;
                // _root.score goes up 1
                unloadMovie(this);
                // this movie clip is unloaded
        }
}

Open in new window



Warm Regards
Deepanjan Das
0
 

Author Comment

by:peter_coop
Comment Utility
thank you deepanjandas. works well. thanks for all your help.
0
 

Author Closing Comment

by:peter_coop
Comment Utility
thank you
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now