[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

as2 increment error

Posted on 2011-03-07
13
Medium Priority
?
359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 1000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
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.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

649 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