Avatar of Mr_Fulano
Mr_Fulano
Flag for United States of America asked on

What is a Sprite?

Hi, I'm new to programming with ActionScript 3 (CS5). I've been trying to learn how the code all comes together and have made some progress, but have also come across a term that is not defined in the material I've been reviewing. The term is "Sprite". The context it's used in is something like this:

import flash.display.Sprite;
public class Projectile extends Sprite {...

So, what exactly is a "Sprite" (other than a soft drink) and how does it work and why would the Projectile class want to extend it?

Thanks,
Fulano
Adobe Flash

Avatar of undefined
Last Comment
Mr_Fulano

8/22/2022 - Mon
OmniUnlimited

According to the Adobe ActionScript Reference:

The Sprite class is a basic display list building block: a display list node that can display graphics and can also contain children.
A Sprite object is similar to a movie clip, but does not have a timeline. Sprite is an appropriate base class for objects that do not require timelines. For example, Sprite would be a logical base class for user interface (UI) components that typically do not use the timeline.

The Sprite class is new in ActionScript 3.0. It provides an alternative to the functionality of the MovieClip class, which retains all the functionality of previous ActionScript releases to provide backward compatibility.

See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Sprite.html?filter_flash=cs5&filter_flashplayer=10.2&filter_air=2.6
ASKER CERTIFIED SOLUTION
Carnou

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Mr_Fulano

ASKER
OK, so based on what you both are stating, a Sprite is somewhat like a layer in Photoshop. It is a container of sorts that holds a graphic of some kind. Its interactive, so I can click on it and get a response or it can listen for events, which in tern may affect its state.

Fulano
Carnou

There are actual layers in Flash, as well, and on each layer you can add multiple sprites.  I would recommend thinking of a sprite as any single item you can display on any layer that is on the stage.

For example, think of a game like Angry Birds. There would be at least two layers- one showing the background, and one showing the birds, pigs, slingshot and defenses. I would make the slingshot and the defenses sprites. They can move and be interacted with, but other than movement, they are not animated. The pigs and birds would have to be MovieClips, though, because they make faces and blink as well as simply moving.

At your introductory level, it may be easiest to think of a Sprite as "any single thing you put on the screen", but use MovieClip instead. You can think of them as essentially the same; the MovieClip is more powerful but less efficient.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Carnou

2 quick things to add to tomaugerdotcom's response:

The code he gave has a typo in line 1.  I wouldn't normally nitpick it, but since you're new at Flash, wanted to be sure you caught it:

var unknownObject:Sprite = Sprite(this.getChildAt(1));

Open in new window


Also, most components like text fields, text input boxes, dropdown lists are descendants of Sprite instead of MovieClip - they can handle keyboard input as well as mouse.  Again, it's for the same reason as tomaugerdotcom explained - they themselves aren't animated, so don't need any part of a timeline.
tomaugerdotcom

@Carnou Ay caramba! Thanks for catching that one.
Mr_Fulano

ASKER
First and foremost, thank you both for the detailed explanations. They have helped me a lot. I'm beginning to see the use for Sprites and how they differ from MovieClips now that you've both given me such good information to consider.

I am learning Flash and ActionScripts 3 from a book and some tutorial videos. They have been very helpful, but there are always questions that remain unanswered and the Sprite issues was beginning to haunt me. I can program in other languages like most of the .NET languages and some Objective-C, PHP and of course XHTML and CSS, but ActionScripts is different. It's a lot less verbose than most of the other languages I've used and its a bit tough to wrap you head around. However, as most other languages, I think there will be am A-Ha moment where everything becomes much clearer...I think I'm getting closer to that moment, but I'm definitely still not there yet. Although, I did pick up Carnou's typo about the ";" instead of an ":".

Thank you both again for the good explanations. I find little use when someone copies and pastes a definition from documentations (which I can read for myself) without offering some sort of layman's version of it or examples, like you two have done.

I suspect that as I move forward with my studies, I'll have a lot more newbie questions for you.

Thanks,
Fulano
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
tomaugerdotcom

I"m sure we'll see you around! For what it's worth, my "AHA moment" came when I actually started to find the ASDoc useful. Up to that point, I couldn't really wrap my head around it and every new thing I tried to do involved what seemed like hours of wasted time researching on Google.

The problem with the documentation is that if you don't know what class you need to do a job, you won't know where to look because the entire thing is ordered by class, and the search function is pitiful. So the best thing you can do (IMHO) is to set yourself some bedtime reading and just try to at least scan through the classes one by one to figure out what they do and how they can be used. Like, would you intuitively think to look at the SoundTransform class to change the volume of a sound? Maybe, maybe not. But it may take a bit of digging before some example code you come across mentions it. So, get a head start and make note of the classes you think will come in handy in the future (like URLRequest, for example, or Loader) and refer back to that list when you have a real-world project!

Good luck!
Mr_Fulano

ASKER
Thanks Tom, I appreciate the advice. The best way I can describe the stage I'm currently in is like when you forget some one's name...you begin to dig though your mind to remember what that person's name was and you begin to recall clues...their name began with an "S", it sounded something like "valley"...oh, yes - her name is "Sally."

I'm at the point where I think I know what the name began with, but I can't yet grasp the rest of it, although it's close...

I don't know if that makes sense, but its actually a similar feeling.

I'm sure I'll have more questions as I continue with my studies. I've been spending at least 2 hours per evening on understanding how to code a simple game. Sometimes it's intuitive, other's its far from it.

Thanks for the help!
Fulano