Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

877327 Posts in 32857 Topics- by 24295 Members - Latest Member: raithza

May 19, 2013, 07:19:08 AM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)AS2 Issues
Pages: [1] 2
Print
Author Topic: AS2 Issues  (Read 2891 times)
Craig Stern
Level 10
*****


I'm not actually all that stern.


View Profile WWW Email
« on: December 25, 2008, 03:24:39 PM »

First of all, merry Christmas to all you Christian types. Wink I've been taking advantage of the holiday to try to learn AS 2, and being the masochist I am, I'm trying to use it to recreate a complicated strategy board game I designed in my college days. I'm having a strange issue that I though some of you might be able to help me with. I have a class (Map.as) that generates a tile-based map, and as part of the function it uses to that, it attaches a movieclip with the instance name "HeroRed" to the stage.

HeroRed is an instance of the Hero movieclip, extended by the class Hero.as. Per the Hero.as class, when I press him, he runs a function that displays a field of clickable blue tiles around him to show his movement range. Here is my problem: no matter what I do, I cannot seem to get HeroRed to actually move when I click those tiles. _root.HeroRed seems to refer to something else, since trace is quite adamant that _root.HeroRed has in fact moved when I click one of the tiles even though the HeroRed movieclip is visibly (and clickably) right where it was.

Does anyone who knows a lot about AS 2 here have any clue what the problem might be? I'd be really grateful for any insight into the matter!
Logged

Glaiel-Gamer
Moderator
Level 10
******


Stoleurface!


View Profile WWW Email
« Reply #1 on: December 25, 2008, 04:57:39 PM »

posting your code would help, post how you attach it and how you try to move it.

Also, if you're learning new why not do AS3? The only reason people complain about it is because they get used to the bad habits that AS2 allows for.

Anyway check for these things (i'm a little shaky on my as2 skills after getting so used to AS3 and c++):

_root.AttachMovie

where are you tracing? What could be happening is:

//move clip
//trace position
//more code
//accidentally move it back
//end of frame
« Last Edit: December 25, 2008, 05:03:06 PM by Glaiel-Gamer » Logged

Craig Stern
Level 10
*****


I'm not actually all that stern.


View Profile WWW Email
« Reply #2 on: December 25, 2008, 07:16:24 PM »

Ah, never mind, I figured it out. I forgot to add the Hero movieclips to the clear map function, and the game was attaching multiple instances of the HeroRed movieclip without clearing off the old ones. Durr...? Sorry!

Anyway, I'm so used to AS1 that I'm having a hard time adjusting even to AS2. I don't want to even think about AS3 until OOP has become comfortable for me!
Logged

Glaiel-Gamer
Moderator
Level 10
******


Stoleurface!


View Profile WWW Email
« Reply #3 on: December 25, 2008, 08:26:11 PM »

in that case it might be worth it to take the jump to AS3 instead of going incrementally through 2 first since 3 is going to force you to forget a couple things you learn about in 2
Logged

Selben Coirlo
Level 8
***


totally awesp,e/

selbencoirlo
View Profile WWW
« Reply #4 on: December 26, 2008, 06:25:30 PM »

One of the things I've learned about AS2 over the past few weeks: All those tutorials where they say it's bad practice to reference _root? Those are TOTALLY RIGHT. Avoid it if at all possible. Same goes double for _level0.
Logged

bateleur
Level 10
*****



View Profile
« Reply #5 on: December 27, 2008, 01:18:35 AM »

in that case it might be worth it to take the jump to AS3 instead of going incrementally through 2 first since 3 is going to force you to forget a couple things you learn about in 2

Seconded.

Also, AS3 is actually much easier than AS2 overall. Even though it might not initially seem so to an AS1 programmer, it simply has fewer nasty gotchas and pitfalls.
Logged

Craig Stern
Level 10
*****


I'm not actually all that stern.


View Profile WWW Email
« Reply #6 on: December 27, 2008, 07:49:22 AM »

Okay, I have a couple of questions for you AS experts out there:

1) What the hell is level0? It keeps saying that one of my movieclips is _root.level0.MCname, and I certainly don't recall creating anything called level0.

2) Can a class both extend movieclips and also inherit from another class? (I'm starting to think that I need separate classes for the different units in this game, but it would be highly efficient if they could all inherit some common functions from a parent "Unit" class.)

As for AS3, what exactly is it that makes it easier to use? By all appearances, it looks much more complicated to perform even basic tasks in AS3 than it does in AS2.
Logged

muku
Level 10
*****



View Profile WWW
« Reply #7 on: December 27, 2008, 09:04:11 AM »

2) Can a class both extend movieclips and also inherit from another class? (I'm starting to think that I need separate classes for the different units in this game, but it would be highly efficient if they could all inherit some common functions from a parent "Unit" class.)

Why not derive a Unit class from MovieClip and let all concrete units derive from this class?
Logged

The Cosyne Synthesis Engine - realtime music synthesis for games
BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #8 on: December 27, 2008, 09:14:42 AM »

1)
_levelx was the mechanism for loading in external movieclips. _level0 is the original flash file as opposed to the ones loaded, while _root was the shortcut for the one you are usually in. Not sure about your exact target.

2)
As muku suggests. Flash does not support multiple inheritance, though interfaces may be appropriate depending on what you intend to do.
You might also want to consider having your movieclips and your Unit objects (whatever they are) seperate, each having a reference to the other. This is often convenient for proper separation of different segments of code.

Why is AS3 better? Well, lots of reasons.
  • The built in libraries have been tightened up to be more consistent, and powerful, in particular the display API.
  • The language itself has improved considerably, though this is difficult to explain unless you try it for yourself, or are already an experienced programmer. Static typing and class based inheritance are very powerful, though you are not obliged to use either.
  • Everyone else uses AS3, so there's better third party library capabilities.
  • It's blazingly fast compared with AS2, and compares pretty well with other scripting languages.

It is a bit more verbose for simple tasks, particularly as tutorials aim to show you the "right" way of doing things. But it pays dividends in larger projects.
Logged
bateleur
Level 10
*****



View Profile
« Reply #9 on: December 27, 2008, 09:19:10 AM »

(Some of this has already been mentioned whilst I was typing, but...)

1) What the hell is level0?

Informally speaking, it's the (base) layer of hardcoded stuff created using the Flash UI drawing tools. If you create everything via ActionScript it never gets used. So presumably you must have placed something on the stage manually?

Quote
2) Can a class both extend movieclips and also inherit from another class?

No.

Once you start designing more complex classes like that (which is a good thing, generally) there are two things which help:

1) Using interfaces. Interfaces are good. Learn to love them.

2) Thinking about the difference between "class Foo is a MovieClip" and "class Foo has a MovieClip". This distinction is important in any OO language, not just ActionScript. You only want to inherit in the former case. Doing so in the latter case is lazy and a habit that can cause trouble for you as your program complexity increases.

Quote
I'm starting to think that I need separate classes for the different units in this game, but it would be highly efficient if they could all inherit some common functions from a parent "Unit" class.

In this scenario a third option would be to have Unit inherit from MovieClip, then individual unit types inherit from Unit.

Quote
As for AS3, what exactly is it that makes it easier to use?

AS2 allows you to cheat in various ways when writing things, primarily by being sloppy with types and properties. It in turn is sloppy internally in the way it does things. All this is superficially convenient, but means that as you write more complex software you will start getting nasty problems associated with this sloppiness (such as runtime type errors, runaway memory consumption, poor performance and so on).

AS3 may cause brief growing pains as you learn to do things properly, but then the compile-time type checking will find a lot of your bugs for you and everything will run faster and be more stable. There are also numerous ease-of-coding benefits due to improved libraries. For example: Not all DisplayObjects are drawable Sprites and not all Sprites are MovieClips. You can load in or draw graphical data to offscreen objects unconnected to your visual hierarchy and then add them anywhere. Clearly tremendously useful for games.
Logged

Craig Stern
Level 10
*****


I'm not actually all that stern.


View Profile WWW Email
« Reply #10 on: December 27, 2008, 10:09:01 AM »

2) Can a class both extend movieclips and also inherit from another class? (I'm starting to think that I need separate classes for the different units in this game, but it would be highly efficient if they could all inherit some common functions from a parent "Unit" class.)

Why not derive a Unit class from MovieClip and let all concrete units derive from this class?

So you can link any number of movieclips to a single class?
Logged

BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #11 on: December 27, 2008, 11:08:03 AM »

Um, you seem to have missed something. If you made Unit a class inheriting from MovieClip, you could have any number of instances of it. And you can also tell all your seperate classes corresponding to your graphics to inherit from Unit instead of MovieClip, making them "concrete" classes of Unit.
Logged
Craig Stern
Level 10
*****


I'm not actually all that stern.


View Profile WWW Email
« Reply #12 on: December 27, 2008, 01:20:20 PM »

So, just to see if I understand this correctly: I can create a Class (Unit) that extends the Movieclip Class generally, then create a bunch of specific unit that extend Unit, and in the Linkage settings for each unit's movieclip, set its Class to the appropriate customized child class. And this will work?
Logged

BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #13 on: December 27, 2008, 02:08:38 PM »

Yes, it will. But I will repeat what everyone has been saying, which is that you should not be doing this. It is unlikely that your Unit class is actually intended to extend Sprite. Do you think you could explain what you mean by Unit?

If it is a game object, is is better that you create a separate object to represent it, and then provide a reference to the Sprite from that. "has-a" vs. "is-a" again.
This separates your game logic from your display code, which will prove vital if you ever intend to change anything, and actually makes your code easier.

So, to give an example.

Code:
public class Unit{

public var health:int;
public var sprite:Sprite;
public virtual function Die() {}
public function Damage(i:int){
  health -= i;
  if(health<0)
    Die();
}
}

public class InfantryMan extends Unit{

public function InfantryMan(){
  super();
  health = 20;
}

public override function Die() {
  sprite.gotoAndPlay("die");
}
}

Here, for simplicity, I am assuming that when you create a new Unit, you also create a sprite for it, and fill in the member variable. But you could also create the appropriate sprite in the constructor for each class too.
Logged
Craig Stern
Level 10
*****


I'm not actually all that stern.


View Profile WWW Email
« Reply #14 on: December 27, 2008, 03:09:01 PM »

Hey Boris, your example code looks very similar to what I was thinking of: I want Unit to provide some basic variables and functions common to all unit types, and use the child classes to fill in the default variables for each unit and perhaps modify some of the Unit functions where a unit requires different treatment.

I see that you don't have Unit extending Movieclip, however. I was thinking of putting movement functions in Unit, which necessarily would have to manipulate the movieclips of the units on the stage. Is there a way to do this without having the Unit class extend Movieclip? Also: shall I assume that a virtual function is a template and an override function is the child class's superseding version of the virtual function? What exactly is the super() function I see in your example code?
Logged

Pages: [1] 2
Print
Jump to:  

Theme orange-lt created by panic