Solved

as2 increment error

Posted on 2011-03-07
13
353 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
ID: 35055923
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
ID: 35056548
hi still the same. the first hit == 05 the second hit == 055 etc.thanks
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35056706
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:peter_coop
ID: 35059174
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
ID: 35063722
Kindly share your fla for debug.

Warm Regards
Deepanjan Das
0
 

Author Comment

by:peter_coop
ID: 35068296
here you go. bear in mind this is just a tutorial file i downloaded to get started. thanks
platformgame.fla
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35069644
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
ID: 35070020
hi. that just creates a timer in the score. it just keeps incrementing. thanks
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35077314
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
ID: 35081454
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
ID: 35085090
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
ID: 35088775
thank you deepanjandas. works well. thanks for all your help.
0
 

Author Closing Comment

by:peter_coop
ID: 35088779
thank you
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Flash Slides/Presnetation 4 74
How to download a youtube video 4 82
merging 12 mp4 files 33 141
Audacity Error message - Ripping a CD to an audio file 8 163
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

810 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