Copy link to clipboard
Copied
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!
this is the only part you add to your class:
MovieClip(root).incrementJumpCounterF();
Copy link to clipboard
Copied
what are you trying to update, JumpCounter? so.data.JumpCounter? something else?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
where is JumpCounter defined? in your document class? on the main timeline? in some other class?
Copy link to clipboard
Copied
In the Main timeline
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
this is the only part you add to your class:
MovieClip(root).incrementJumpCounterF();
Copy link to clipboard
Copied
I did that, but then got that error. Why?!
Copy link to clipboard
Copied
Nevermind!!! I tweaked a bunch of stuff. Got it! Thanks!
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now