ForumsProgramming ForumBitmap - Line/Trail From Object

6 3623
Klemzo
offline
Klemzo
14 posts
Nomad

Heya.
I playing around with a "space-shooter" game and I have this script that goes into the first frame, to make the player (hero) appear on the stage. The script then adds a bitmap of a line/trail that goes from the player (hero) when he moves (like if its a ship, a fire/smoke path or whatever).
So this comes directly to the first frame, on the root timeline:
&ltre>
_root.attachMovie("hero", "hero_on_stage", 10000,{_x:200,_y:20});
_root.attachMovie("trail_sprite", "trail_mc", 8000);
trailbitmap = new flash.display.BitmapData(500, 350, true,0x000000);
_root.createEmptyMovieClip("trail", 1);
trail.attachBitmap(trailbitmap, 0);
trail_mc._visible = false;
</pre>

Then it calls the hero on stage:
&ltre>
hero_on_stage.onEnterFrame = function() {
</pre>

... and with all the other script (that makes him move, shoot and whatever), then comes this to define the trail:
&ltre>
_root.trail_mc.trail_sprite._x = this._x;
_root.trail_mc.trail_sprite._y = this._y;
_root.trailbitmap.draw(_root.trail_mc);
trail_rectangle = new flash.geom.Rectangle(0, 0, 500, 350);
trail_blur = new flash.filters.BlurFilter(2, 2, 3);
_root.trailbitmap.applyFilter(_root.trai lbitmap, trail_rectangle, new Point(0, 0), trail_blur);
}
</pre>

Ok, so this works perfectly. Then just just have trail_shape and trail_sprite movie clips and it works fine.
-but-
The problem is that I don't want the frame to call in (attach) the player, but I want for me to set the player (with my mouse) on the stage. And I would script the player movie clip instead of the frame (don't ask my why :P).
Now I don't know how to transfer this code, so that it works perfectly as here.
I manage something, but the trail looks ugly with huge pixels and it "lags" and moves weirdly/slowly.
I tried countless of things but can't get it to work.

Any help appreciated.
Thanks

"Let the light or me be with you"

  • 6 Replies
Klemzo
offline
Klemzo
14 posts
Nomad

Does noone know?

Klemzo
offline
Klemzo
14 posts
Nomad

Bump

PixelSmash
offline
PixelSmash
566 posts
Nomad

Ahh... well, as far as why it's so slow, I can give you an easy answer; the blur. Blurring is quite expensive as far as CPU, so you should try to avoid calling it every frame... it'll definately kill your framerate!

As far as your other questions: I'm totally over to the other side (being: AS3) so I can't give you exact coding examples. I'll try to describe some stuff though, perhaps with some pseudo-code, and hopefully in doing so you'll be able to figure out how to do it yourself!

First off... I don't really get what it is exactly you mean by setting the player on the stage with the mouse... I'm guessing it's either after a click, or updating the player position every time you move the mouse.
If it's the first, you should add some sort of mouse click listener to the root &ltseudo> _root.onMouseClick </pseudo> and in that function add the player. Make sure you remove it afterwards though, otherwise you'll keep adding players, and I doubt that's what you want!
If it's the latter, you should go with your initial setup (the &ltre></pre> stuff), and add the code for moving the player on a frame inside the player mc. Basically what you did right now, only one layer deeper.

Now I don't know this for sure, I might be wrong, but you could be off a whole lot better if you added the blur to the trail_mc in the setup, rather than doing so every frame. This should give quite a performance increase!

As I said earlier, if you want to transfer the player code from the main timeline to the player itself, you should paste it in a frame inside the player mc. Keep in mind though, you won't need to use hero_on_stage.onEnterFrame, I believe onEnterFrame is good enough then. This should refer to the object in which the code is contained - add it to the _root, and it'll be code relative to the _root, add it to the player and it'll be relative to the player.

By the way, you say your trail is ugly and pixelated... It'll probably have something to do with the blurring, but by looking at your code, isn't the rectangle you draw for the trail absolutely huge? 500x350 is quite a lot, I'd say!

Let me know if this works, and if you have other (or new) questions, drop a line!

Cheers!

Klemzo
offline
Klemzo
14 posts
Nomad

PixelSmash, I'm so excited of reading what you've posted but don't have time right now. I'll read it when I come back.
Till then I just want you to know that I didn't meant what you thought "by mouse". I meant that you can place the hero on the map before the game (me only, the maker of the game), so I don't have to use scripts to locate it :P

PixelSmash
offline
PixelSmash
566 posts
Nomad

Ahh, I see. Well, you'll have to do some sort of placement whatever you want to do All the other scripts can be placed inside the player, but if you want it centered on the stage, and you want it done by AS... you'll need a bit of code on your timeline

Gawd, I can't even remember how to work with classes in AS2... sorry!

Klemzo
offline
Klemzo
14 posts
Nomad

Hehe, no, I do mean to place the script in player.
The script I gave in first post is to be posted in main timeline frame.
Well I just want that script made so that I can post it in player, that's all

Showing 1-6 of 6