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
Solved

as2 increment error

Posted on 2011-03-07
13
354 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
System Center Configuration Manager 2012R2 7 90
Video Annotation Software 3 62
How can I convert RealMedia (.rm) video files to MP4? 5 97
How to view a .vls video file 3 56
There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
The goal of the tutorial is to teach the user how to how to record live broadcast.
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.

860 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