-
[AS2] Won't remove and replace MovieClip when key is pressed
Hi everyone. This is my first appearance here. I'm interested in using AS2 to learn object-oriënted programming, and one of the methods I think I will use the most is to develop games. Now, I'm trying to make a up-down-left-right character control, but the character will always appear on the centre, only changing oriëntation. (You can see this method of character control in games like Pokémon).
2015%u00252B04%u00252B25%u00252BFLASHHELP.png
Code:
this.attachMovie('charup', 'karakter', 1);
karakter._x = 250;
karakter._y = 170;
if (Key.isDown(Key.LEFT)) {
this.removeMovieClip('karakter');
this.attachMovie('charle', 'karakter', 1);
backgro._x = backgro._x + 50;
}
I tried to compile this script but what I get is that the lines 1-3 worked seamlessly, but the subsequent lines didn't work. I wonder what is wrong. I have a couple of books relating to Flash 8 game development (which I utilised in my childhood to create Flash games too, but just by copying the code on the CDs given though) and I think I will have much time to spend doing this on the upcoming college holiday. Now I would like to master the programming (I have outlined a simple algorithm of the AI, how would they respond if they get attacked, how will objects that move constantly have no problems moving when the background moves as a result of the player moving). I can make the simple algorithms but I'm still blind to the syntaxes.
I'm looking for responses and I'll appreciate any assistance given.
Thank you.
-
.
Hi,
I didn't really understand all that you wrote, but you need to add a listener to your key events, similar to:
PHP Code:
this.attachMovie('charup','karakter',1); karakter._x = 250; karakter._y = 170;
var keyListener:Object = new Object();
keyListener.onKeyDown = function() { if (Key.isDown(Key.LEFT)) { removeMovieClip('karakter'); attachMovie('charle','karakter',1); backgro._x = backgro._x + 50; } };
Key.addListener(keyListener);
-
Originally Posted by fruitbeard
Hi,
I didn't really understand all that you wrote, but you need to add a listener to your key events, similar to:
PHP Code:
this.attachMovie('charup','karakter',1);
karakter._x = 250;
karakter._y = 170;
var keyListener:Object = new Object();
keyListener.onKeyDown = function()
{
if (Key.isDown(Key.LEFT))
{
removeMovieClip('karakter');
attachMovie('charle','karakter',1);
backgro._x = backgro._x + 50;
}
};
Key.addListener(keyListener);
Hmm, got it, so it won't work without the listener.
Now that the character can move freely, but its orientation didn't change.
My idea is to remove the old sprite on the stage and replace it with the new one using the same reference name (karakter).
I've ticked the checkbox "Export for ActionScript" and "Export for Frame 1", though.
Code:
this.attachMovie('charup', 'karakter', 1);
karakter._x = 250;
karakter._y = 170;
var keyListener:Object = new Object();
keyListener.onKeyDown = function()
{
if (Key.isDown(Key.LEFT)) {
this.removeMovieClip('karakter');
this.attachMovie('charle', 'karakter', 1);
backgro._x = backgro._x + 50;
}
if (Key.isDown(Key.RIGHT)) {
this.removeMovieClip('karakter');
this.attachMovie('charri', 'karakter', 1);
backgro._x = backgro._x - 50;
}
if (Key.isDown(Key.UP)) {
this.removeMovieClip('karakter');
this.attachMovie('charup', 'karakter', 1);
backgro._y = backgro._y + 50;
}
if (Key.isDown(Key.DOWN)) {
this.removeMovieClip('karakter');
this.attachMovie('chardo', 'karakter', 1);
backgro._y = backgro._y - 50;
}
};
Key.addListener(keyListener);
-
.
HI,
You might be better off creating an empty movie clip to attach your character to, that way you can just over write the image inside of it.
You might want to also think about telling the character clip to go to a certain frame instead of always attaching, up to you.
PHP Code:
clipHolder = this.createEmptyMovieClip("characterContainer", 1);
clipHolder.attachMovie('charup','karakter',0);
karakter._x = 250;
karakter._y = 170;
var keyListener:Object = new Object();
keyListener.onKeyDown = function()
{
if (Key.isDown(Key.LEFT))
{
//this.removeMovieClip('karakter');
clipHolder.attachMovie('charle','karakter',0);
backgro._x = backgro._x + 50;
}
if (Key.isDown(Key.RIGHT))
{
//this.removeMovieClip('karakter');
clipHolder.attachMovie('charri','karakter',0);
backgro._x = backgro._x - 50;
}
if (Key.isDown(Key.UP))
{
//this.removeMovieClip('karakter');
clipHolder.attachMovie('charup','karakter',0);
backgro._y = backgro._y + 50;
}
if (Key.isDown(Key.DOWN))
{
//this.removeMovieClip('karakter');
clipHolder.attachMovie('chardo','karakter',0);
backgro._y = backgro._y - 50;
}
};
Key.addListener(keyListener);
You could always consider using a switch statement instead of all the if's too.
You might also want to think about using AS3 instead of AS2 as it is far superior and you can also end up doing mobile/tablet stuff
-
Originally Posted by fruitbeard
HI,
You might be better off creating an empty movie clip to attach your character to, that way you can just over write the image inside of it.
You might want to also think about telling the character clip to go to a certain frame instead of always attaching, up to you.
PHP Code:
clipHolder = this.createEmptyMovieClip("characterContainer", 1);
clipHolder.attachMovie('charup','karakter',0);
karakter._x = 250;
karakter._y = 170;
var keyListener:Object = new Object();
keyListener.onKeyDown = function()
{
if (Key.isDown(Key.LEFT))
{
//this.removeMovieClip('karakter');
clipHolder.attachMovie('charle','karakter',0);
backgro._x = backgro._x + 50;
}
if (Key.isDown(Key.RIGHT))
{
//this.removeMovieClip('karakter');
clipHolder.attachMovie('charri','karakter',0);
backgro._x = backgro._x - 50;
}
if (Key.isDown(Key.UP))
{
//this.removeMovieClip('karakter');
clipHolder.attachMovie('charup','karakter',0);
backgro._y = backgro._y + 50;
}
if (Key.isDown(Key.DOWN))
{
//this.removeMovieClip('karakter');
clipHolder.attachMovie('chardo','karakter',0);
backgro._y = backgro._y - 50;
}
};
Key.addListener(keyListener);
You could always consider using a switch statement instead of all the if's too.
You might also want to think about using AS3 instead of AS2 as it is far superior and you can also end up doing mobile/tablet stuff
Do I have to make a movieclip named "characterContainer" first?'
Hmm, isn't AS3 much more complicated? Or should I learn it from scratch?
Actually, I have Flash CS6 to get my project finished with.
-
.
Hi,
No, "characterContainer" has already been created, dynamically, hence the code (createEmptyMovieClip)
It is just being referenced with the name clipHolder.
I will leave it up to you to decide about AS3 or not, it is slightly more verbose and long windied sometimes, yes, but the logic shies through.
-
Originally Posted by fruitbeard
Hi,
No, "characterContainer" has already been created, dynamically, hence the code (createEmptyMovieClip)
It is just being referenced with the name clipHolder.
I will leave it up to you to decide about AS3 or not, it is slightly more verbose and long windied sometimes, yes, but the logic shies through.
I think I'm going to go to AS3 since I have already purchased Flash CS6.
But I'm still curious about some aspects of AS2 before moving on to the higher version.
I'm trying to make a text displayer that will disappear about 2 seconds.
Code:
var textA:String;
var textB:String;
var textDelTimer:Number = setTimeout(textDelTimer, 2000, delText)
function displaytext(){
this.createTextField(instanceName="textFieldA", this.getNextHighestDepth(), 0, 20, 500, 60);
textFieldA.selectable = false;
textFieldA.text = textA;
this.createTextField(instanceName="textFieldB", this.getNextHighestDepth(), 0, 20, 500, 120);
textFieldB.selectable = false;
textFieldB.text = textB;
delText(textFieldA);
delText(textFieldB);
}
function delText(){
this.removeTextField();
}
textA = "hello";
textB = "world2";
Although there were no errors when compiling, the texts don't even appear.
I want to make it available in Scene 1.
I would like the text to look somehow like this.
SCRMEGA.jpg
maomao3.PNG
-
.
Hi,
PHP Code:
var textA:String; var textB:String;
function displaytext() { this.createTextField("textFieldA",this.getNextHighestDepth(),0,20,500,60); textFieldA.selectable = false; textFieldA.text = textA;
this.createTextField("textFieldB",this.getNextHighestDepth(),50,20,500,120); textFieldB.selectable = false; textFieldB.text = textB; //delText(textFieldA); //delText(textFieldB); setTimeout(delText,2000); }
function delText() { textFieldA.removeTextField(); textFieldB.removeTextField(); }
textA = "hello"; textB = "world2";
displaytext();
You were not calling the create textfields function > displaytext();
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|