as3 registration point

9 posts (showing 1-9)
pranadevil

Market Level 0Community Level 3
133 posts

hi is there a way to change the bitmap's registration point in AS3 to be not at left upper corner(default), instead its center?

any help will be awesome

posted 2016-03-08T00:47:48-08:00
DelaniStudio

Market Level 2Community Level 5
370 posts

Create movie clip, in that movie clip place your bitmap,and just divide height and width coordinates by 2, and you got centered bitmap

posted 2016-03-08T01:53:25-08:00
JohnBlackburne

Market Level 3Community Level 10
2184 posts

You don’t need a MovieClip, a Sprite will do and is  more lightweight.

var s:Sprite = new Sprite();

s.addChild(myBitmap);

myBitmap.x = myBitmap.width >> 1;

myBitmap.y = myBitmap.height >> 1;

then just use the Sprite s in place of the Bitmap.

posted 2016-03-08T02:47:04-08:00
pranadevil

Market Level 0Community Level 3
133 posts

by doing this, who gonna manage the mouse events? the sprite or the bitmap?

posted 2016-03-09T19:06:49-08:00
JohnBlackburne

Market Level 3Community Level 10
2184 posts

In my code neither. There can be a significant issues with doing events per-object, i.e. by attaching listeners to everything and doing the event processing per-object.

One issue is performance as handling Flash mouse events can require it to do a series of  hittests per click which can get quite expensive. This depends on the game and the platform it is run on – a slower PC will be worse – but better to avoid it altogether.

A more subtle one is not knowing when the user can click, leading to all sorts of subtle and hard to fix bugs. E.g. a user can click on something while it is being processed, maybe while it is being deleted, only to find their click does nothing or even worse runs functions on an object you thought was removed. This can be very rare but that only makes it harder to fix.

The solution to both is have a single event listener for the whole game. Don’t handle events immediately but put them in a queue to be processed when it is safe such as at the start of the frame. On a PC there will normally be only one mouse event per frame, though there can be more than one touches at once in a mobile game.

To decide what is being clicked on write your own  logic. This is a bit more work but it will generally produce much more efficient code as you know what things in the game are important and how they are related. Often you can so better than the built in hittest code, such as by using more logical hit boxes (how many games do you know that have items that are frustrating to click on because they have complex sprites with lots of holes).

posted 2016-03-09T22:46:57-08:00
pranadevil

Market Level 0Community Level 3
133 posts

Ok thanks for the reply but imagine a game in which we got several objects in the stage at the same time. I want to know when the mouse is over one of them.

So the best way is to add the listener to the object itself ?

posted 2016-03-10T00:17:59-08:00
JohnBlackburne

Market Level 3Community Level 10
2184 posts

The way I do it is have an object that covers the whole game. E.g. the stage, or the background. Add any MouseEvent listeners to that. Then disable mouse events on all other objects, by e.g. setting their mouseEnabled to false, so all clicks ignore them and reach my listener.

I queue up events then process them myself. E.g. if the objects are rectangles I might use Rectangle:contains or Rectangle:containsPoint. For a circular target I just check how far the click is from the centre and compare it to the radius. And so on.

I do not query the graphics data when doing this. The graphics represents the game but it is not the game logic. This means I can change rendering methods without worrying the gameplay will change. This formally is a model-view-contoller approach, done mostly for my own sanity to stop the code degenerating into a complete mess.

posted 2016-03-10T04:01:39-08:00
pranadevil

Market Level 0Community Level 3
133 posts

ok thanks man!

posted 2016-03-10T14:56:28-08:00
9tween

Market Level 0Community Level 1
103 posts

yeah, add mouse down event to stage then use e.target for multiple objects on stage

as for hit test is concern use pixel perfect class for complex shapes

posted 2016-05-25T03:47:39-07:00