Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Incrementing Counter on Stage through Class

New Here ,
Feb 02, 2013 Feb 02, 2013

Hello everyone, I have a thread similar to this one about saving and loading a counter. However, I can't get the counter to increment when colliding with an object. When colliding with the object, it makes it not visible so you can't see it anymore.

My counter that saves and loads looks like this:

var so:SharedObject = SharedObject.getLocal("myStuff","/");

var JumpCounter: int = 0;

if(so.data.JumpCounter)

{

          JumpCounter = so.data.JumpCounter;

}

And when you jump:

  JumpCounter++;

          so.data.JumpCounter = JumpCounter;

          so.flush();

It ouputs here to a text box:

JumpBox.text = JumpCounter.toString();

This all works fine, I just can't get it to increment when colliding with my MovieClip.

I have a class that looks like this attached to my MovieClip that I want to increment that looks like this:

package {

          import flash.display.*;

          import flash.events.*;

          public class AddJumps extends MovieClip{

                    //construct function

                    public function AddJumps():void

                    {

                              addEventListener(Event.ENTER_FRAME, collision);

                    }

                    private function collision(e:Event):void{

                              if(this.hitTestObject(MovieClip(root).Player)){

                              MovieClip(root).JumpIcon.visible = false;

                              }

                    }

          }

}

I'm unsure as how to access or increment my counter from within a class. I can't seem to remember how to access the stage.

Thanks in advance!

TOPICS
ActionScript
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 02, 2013 Feb 02, 2013

this is the only part you add to your class:

MovieClip(root).incrementJumpCounterF();

Translate
Community Expert ,
Feb 02, 2013 Feb 02, 2013

what are you trying to update, JumpCounter?  so.data.JumpCounter?  something else?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 02, 2013 Feb 02, 2013

Both, technically. I want the counter to increment when I collide with the object that contains the class. I can't seem to remember how to access the stage or something on the stage from within a class.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 02, 2013 Feb 02, 2013

Basically I just want it to add 1 to the counter when it collides. I just don't remember how to access certain things from the stage.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 02, 2013 Feb 02, 2013

where is JumpCounter defined?  in your document class?  on the main timeline?  in some other class?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 02, 2013 Feb 02, 2013

In the Main timeline

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 02, 2013 Feb 02, 2013

on your main timeline you should use:

var so:SharedObject = SharedObject.getLocal("myStuff","/");

var JumpCounter: int = 0;

if(so.data.JumpCounter)

{

          JumpCounter = so.data.JumpCounter;

}

function incrementJumpCounterF():void{

  JumpCounter++;

          so.data.JumpCounter = JumpCounter;

          so.flush();

}

//And when you jump (or otherwise want to increment JumpCounter from the main timeline or a class like AddJumps):

MovieClip(root).incrementJumpCounterF();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 02, 2013 Feb 02, 2013

Alright, so I added that to the class and got an error.

ArgumentError: Error #1063: Argument count mismatch on Game_fla::MainTimeline/incrementJumpCounterF(). Expected 1, got 0.

          at CountJumps/collision()

I just need it to add 1 to the counter. Is there an easier way to do that?! You collide with an icon and it increments the counter by one. Thats alllllll

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 02, 2013 Feb 02, 2013

this is the only part you add to your class:

MovieClip(root).incrementJumpCounterF();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 02, 2013 Feb 02, 2013

I did that, but then got that error. Why?!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 02, 2013 Feb 02, 2013

Nevermind!!! I tweaked a bunch of stuff. Got it! Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 02, 2013 Feb 02, 2013
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines