Copy link to clipboard
Copied
So I'm working on a pretty basic game at the moment. When I press the down key, I want the character (at the moment, just a square) to slide underneath obstacles. I have the standing animation as one frame and the sliding as another within the same class, but when I press the down key, it switches to the second frame yet another object on the first frame remains over top. I suspect that because I created the square like so:
//Create square
var newSquare:Number = 1;
var Square:square = new square;
Square.x = 50;
Square.y = 180;
stage.addChild(Square);
that it's constantly creating new squares overtop the one that's sliding. The code for the sliding is as follows:
//Slide
var down:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, _slideDown);
function _slideDown(e:KeyboardEvent):void {
if (e.keyCode == 40) {
down = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP, _slideUp);
function _slideUp(e:KeyboardEvent):void {
if (e.keyCode == 40) {
down = false;
}
}
stage.addEventListener(Event.ENTER_FRAME, _slide);
function _slide(e):void {
if (down == true) {
Square.gotoAndPlay(2);
}
if (down == false) {
Square.gotoAndPlay(1);
}
}
If I'm right about generating infinite squares, could someone help me fix it? I tried using a conditional and number variable and then changing the number when the square was created, but that didn't help. If I'm not right, any other help would be appreciated.
Copy link to clipboard
Copied
When you create things dynamically, they do not have a home in a timeline unless you make them a child of an object that is secured to the timeline.
Copy link to clipboard
Copied
First off, sorry for the late response.
I don't see why that would be an issue here. I went ahead and made the second frame very transparent, which confirmed that the problem is that it continues to generate Square classes overtop the old ones. I tried making Square a child of another object, but that didn't fix anything.
Copy link to clipboard
Copied
You should get a Bunch of errors with that Code.
Enable debugging (File->Publishing Options->Allow Debugging) and look in the Output Window, what it says.
There are 2 major errors in your 2nd Line of Code
Scene 1, Layer 'Layer 1', Frame 1, Line 5 | 1046: Type was not found or was not a compile-time constant: square. |
Scene 1, Layer 'Layer 1', Frame 1, Line 5 | 1180: Call to a possibly undefined method square. |
Your code is obviously not working as intended.
A human can interpret what you want, but the compiler can not:
var newSquare:Number = 1;
// You have the idea to somehow create 1 square and heard that in OOP its done with new. But this line instead creates a Number (not one square)
var Square:square = new square;
// here you are trying to make another square and somehow thinking you could refer to the variable in Line 1, but instead of writing newSquare you write new square. Two totally differnent things for the compiler.
//and even if you would have written it correct as newSquare without the space and casesensitiv with a big S the compiler wouldn`t get it because an Object of type "square" can´t be number
With these two lines at the start you have no hope of getting further.
Start small, be exact and forget about the listeners at all.
Try only to achieve what you set out to do in your first Line:
//Create square
Copy link to clipboard
Copied
I'm actually not getting any errors at all; the square is being generated as it's supposed to, the only problem being when I try to change frames.
What I was doing with the number was trying to use its value as a conditional, generating a square and then changing the value so no more squares would be generated, but that didn't work. Changing
var Square:square = new square;
to
var Square:square = newSquare;
gives me the error of "Access of unidentified property newSquare."
Copy link to clipboard
Copied
I'm actually not getting any errors at all
Then this must be a special compiler. Can I have one of those, too?
Sorry, just kidding.
Google: "as3 how to create MovieClip" and try to apply this to your project.
Mind your spelling when you copy code examples!
(Programming is Science not Literature)
Copy link to clipboard
Copied
You should get a Bunch of errors with that Code.
Enable debugging (File->Publishing Options->Allow Debugging) and look in the Output Window, what it says.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now