Foreground for Flash games - actionscript-3

I've noticed that most Flash games (and other SWFs) have foregrounds incorporated into them. The reason being to cover up graphic you're not supposed to see upon entering full screen (graphic outside the boundaries of the stage).
The foreground is simply a piece of graphic with a stage-sized hole in the middle, where the stage will show through. (Or that is what it seems to be, anyway).
What would be the simplest way to incorporate something like this into a Flash game? (In terms of highest childIndex etc). All help appreciated.

Masking is the best solution for this. You have to simply create a shape (rectangle or even circle or a complex set of drawing) and then setting it as mask. Then The masked viewport only shows the area of the mask's shape.
Take a look at this
http://www.republicofcode.com/tutorials/flash/as3masking/

stage.addChildAt(myMC,stage.numChildren-2);
This will add myMC to the stage at the z level that is just below the highest z-order (which should be where you put your border).
To explain further, numChildren returns the number of children that the DisplayObject it is called on has. But remember, the z-order index is "zero based" which means that if the parent display object has 12 children, the numChildren method will return 12, but the highest z-order index will be 11 and the first will be 0. So the highest z-order child will have an index of 'numChildren-1(this could be where the mask/border) and the highest level below that would benumChildren-2`.

Related

Can I colorTransform a MovieClip but exclude specific sub-components?

Scenario:
I have floating islands that move around in odd paths. There are torches (nested movieClips) mounted on some of the islands. Sometimes a weather effect will change the color and brightness of the islands. Is there a way to change the brightness of the islands (parent movieClips) without also changing the brightness of the torches (child movieClips)?
===================================================
One solution that I can conceive is independently color adjusting all the child objects of each movieClip, instead of adjusting the parent object. This seems highly inconvenient and unnecessary though, so any thoughts would be appreciated.
No, if you need to exclude some parts of a movie clip (or other DisplayObjectContainer) from alteration applied to their parent, you need to rework your container structure. You can try applying a reverse operation to selected children (say, inverse color transformation), but note that not every transformation can be correctly inversed.

cocos2d, reorder children in different batch node

I am creating a fighting game. I've got a spritesheet for the hero and a spritesheet for a monster. Since both will be able to do multiple attacks I would like to avoid to merge them in a single spritesheet. And I am intending to create more characters.
Now I am creating a SpriteBatchNode for each spritesheet and add them to layer.
CCLayer* stage = CCLayer::create();
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("character_hero.plist");
this->characterHeroBatchNode = CCSpriteBatchNode::create("character_hero.pvr.ccz");
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("character_monster.plist");
this->characterMonsterBatchNode = CCSpriteBatchNode::create("character_monster.pvr.ccz");
stage->addChild(characterHeroBatchNode);
stage->addChild(characterMonsterBatchNode);
Now I have one sprite in characterHeroBatchNode and multiple in characterMonsterBatchNode.
How can I reorder the monsters and the heros z-Order based on their PositionY attribute.
For example>
monster1->setPositionY(10); // In monster batch node
hero->setPositionY(24); // In hero batch node
monster2->setPositionY(43); // In monster batch node
I want the monster 1 behind the hero. And the hero behind monster2.
In the past, I've had to create games where I wanted some sprites in the foreground and others in the background. The sprites were in different sheets, like you have them.
To get them in the order I wanted, I put the "front" sprites into one CCLayer and the "back" sprites into another CCLayer. I added the layers to the scene in the order I wanted them to appear. I also manipulated each the respective layers to make them fade in/out as needed. So you could have the hero in the scene and then have the enemies appear behind him.
If you want to mix and match, you can have 3 layers, one for the "middle ground", one for "front", and one for "back", and dynamically move the sprites between the layers.
Was this what you were looking for?
If I understand what you're asking, the answer is, you can't. Consider each CCSpriteBatchNode to be its own container. You can adjust the Z order of the sprites inside the CCSpriteBatchNode, but when you add the batch to the CCLayer the whole CCSpriteBatchNode is applied to the CCLayer in whatever Z order you added it at. So in order to do what you want you would need another batch of monsters. Or use CCSprites and add/adjust them on the CCLayer in the way you want, but then you obviously lose the CCSpriteBatchNode benefits.

cocos2dx - sub layer that covers its parent partially keep capturing all touches

In my cocos2dx game, I have a CCLayer that contains another CCLayer. The sublayer just cover part of the container layer. I 'think' I achieve this through:
this->setContentSize( CCSizeMake( 100, 200 ) );
however, the sublayer always capture touches even though it is outside its size and position area... Is it common?
I can filter through the touches position by comparing it inside the ccTouch** functions, but I think it is a hack, what is the proper way to set the sublayer to properly cover just the partial area of its parent?
The only thing i can think of straight away is making this inner layer a CCNODE and also extent it with CCTouchDelegate.
Now with this, when u register with the TouchDispatcher, you make sure it doesn't Swallowtouches(the boolean value given as the last parameter)...
This way when you receive a touch ... just see if it is within the boundary of this inner layer of urs and if it is not, send let the parent class use this touch.
Hope this helps.

Getting graphic/movie clip x,y position from within another movieclip?

This should be fairly simple I'd think, I'm just not that familiar with actionscript haha.
I have a game where I have the background moving behind a character that stays in one position on screen. I'm relatively new to actionscript 3 but I'm wanting to have text boxes pop up whenever the player presses a key over certain objects passing in the background.
So, basically the background itself is a movie clip, and I have other graphics and movie clips within the background mc.
I was thinking of getting the player.x and y position and then "comparing" that position (>= and <=, etc.) with the graphic/movie clip in the background. But I just don't know how to obtain the x and y coordinate of the graphics/movie clips in the background mc.
You could try to target your movie clips in the background by getting their coordinates, then removing their parent's position (the background container).
Something like :
var finalXPosition:int = targetMovieClip.x - backgroundContainer.x;
var finalYPosition:int = targetMovieClip.y - backgroundContainer.y;
By substracting the target movieclip parent's position to its position, you gain the final position in the parent's scope coordinates.
It should work for you as soon as your character and your background container are situated at the same level of the display list.
Here is a quick diagram of what I try to explain (please forgive my inaptitude to draw nice and explicit drawings ^^)
Usually, when I stumble upon such a case, I try to make a quick and even dirty drawing, starting with what I want, then breaking down every useful data I have to achieve that result, you should keep that method in mind and try it the next time ! :-)

ActionScript Measuring 3D Depth

i'm having a difficult time understanding how to control the z property of display objects in a 3D space. i know how depth works, but what i don't understand is how i can get the maximum depth, or the number at which the display object just disappears into the background.
i assume depth is based on the stage's width and height, and that is why assigning the same depth of the same display object appars mismatched with different stage sizes.
so how can i appropriately measure depth?
You need to consider the childIndex property. There is no Z-index in actionscript.
To get the depth you could use:
// returns the number of direct display children in stage
stage.numChildren;
// returns the number of direct display children in you object
myObj.numChildren;
To set the child Z-index use
//sets the newIndex of child in stage
stage.setChildIndex(child:DisplayObject, newIndex:int):void;
If newIndex is 0 then child is top visible element.
newIndex must be in [0, numChildren-1] else flash will throw errors
Take care.
As of Flash 10, there is a 'z' property.
Checkout this link for a tutorial:
http://www.kirupa.com/developer/as3/intro_3d_as3_pg1.htm
it's explained here: Perspective in Flash