Arrange the sprites and text on the screen with Libgdx - libgdx

I'm in the process of building my splash screen and I'm having some doubts related to align the elements and fit them right. How can make my sprites, text and other elements to occupy the right place and make them align on the screen, if you know what I mean. Should I use Table for that? How can I do that and how can I use the Table for this operation?
I changed my code: now may Player class extends the Actor class.
But I'm getting some troubles because I added the player into the table but the player image appears almost in the middle of the screen. It wasn't happening before I have inserted the method align the table to the center, but I removed this method and the player still keeps with that position on the screen
A part from the SplashScreen in the show() method:
// Create the SpriteBatch
this.game.spriteBatch = new SpriteBatch();
// Create the stage
this.game.stage = new Stage(this.game.viewPort);
// Create the table
this.table = new Table();
this.table.setFillParent(true);
this.game.stage.addActor(this.table);
// Insert the elements into the table
this.table.row();
this.table.add(this.game.player);
Now from the render() method from the SplashScreen:
// Set the projection matrix for the SpriteBatch
this.game.spriteBatch.setProjectionMatrix(this.game.orthoCamera.combined);
// SpriteBatch begins
this.game.spriteBatch.begin();
// Display the ClimbUp logo
this.gameTitle.draw(this.game.spriteBatch);
this.table.draw(this.game.spriteBatch, 1);
this.game.player.draw(this.game.spriteBatch, 1);
// SpriteBatch ends
this.game.spriteBatch.end();

Yes, when it comes to alignment in LibGdx Table is the way to go. Using Table's is very easy.
There first thing you need to have is a Skin instance which takes a TextureAtlas parameter, then you can instantiate the Table.
Table someTable = new Table(); //Takes a Skin object as a parameter.
someTable.add(); // Element as parameter.
someTable.row(); // Add a row if you need it.
someTable.add(); //Element as parameter.
someTable.debug(); // Don't forget to call debug();
When you have the Table object instantiated and ready to go just add it to a Stage object and let it do the drawing and other stuff.
someStage.addActor(someTable);
But before doing any of this you should probably take a look here.

Related

How to hide mouse over multiple frames?

I'm an actionscript n00b, please excuse my ignorance.
currently trying to make my first person shooter game and have picked up great tips from Lynda(.com) but im having difficulty changing my mouse into a crosshair cursor and having that crosshair not show up permanently in subsequent frames as I create this game.
so i have this code on an action layer but on other layers when I create another key frame to change what I shoot at on other layers, my cursor stamps a permanent image of itself onto the screen. I definitely need to understand this language more if Im going to become good at it but for now i'f like to be able to make a few games to entertain myself, any suggestions? are appreciated
var cursor:MovieClip;
function initializeGame():void
{
cursor = new Cursor();
addChild(cursor);
cursor.enabled = false;
Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
}
function dragCursor(event:MouseEvent):void
{
cursor.x = this.mouseX;
cursor.y = this.mouseY;
}
initializeGame();
In AS3, items will remain on the display list (on screen) until explicitly removed (or their parent is removed).
So to keep your cursor from staying on screen, you need to remove it:
removeChild(cursor);
Doing Mouse.show() doesn't affect any display objects on the screen.
If you assign a new value to the var cursor, it doesn't automatically remove the previous object - as variables are just references to objects. So if in your subsequent frames you are making a new cursor, you want to remove the existing one prior to assigning a new one to the variable:
if(cursor.parent) removeChild(cursor); //if there is an object assigned to cursor, and it has a parent (which means it's on screen), then remove it before doing the below code
cursor = new Cursor();
addChild(cursor);

Add Child Behind existing object AS3

I have made a game using Flash CS5 (as3) and I am trying to add a child to the stage behind objects that are already there. For example, I have a bar at the bottom of the screen that is there from the time you start the game and I have falling objects that I want to fall behind it, but instead they fall in front of it because I added them to the stage after. Is there a way of adding the falling objects behind it without having to keep re-adding the bar to the stage? Thanks in advance.
Rather than layering, I'd use adjust the index of each object using addChildAt and setChildIndex.
The following line adds your falling object behind every other DisplayObject on the stage (in this case, you should probably add your bar to the stage first)
stage.addChildAt(fallingObject, 0);
You can create Sprites to act as layers, and add the different objects to them. Here is an example that adds a layer for adding whatever you want behind the bar layer, and THEN adds the bar layer, so it will be on top. It's super rough since you don't have any code to reference:
package {
import flash.display.*;
public class Test extends MovieClip {
var barLayer:Sprite;
var objectLayer:Sprite;
public function Test() {
var objectLayer = new Sprite();
//add the object layer to your main Class
this.addChild(objectLayer);
//now you can add movie clips or sprites to objectLayer whenever you like
//then create the bar layer
var barLayer = new Sprite();
//add your bar Display Object here
var bar = new MovieClip();//draw the bar or reference the library object you've created
barLayer.addChild(bar);
//now add the barLayer
this.addChild(barLayer);
}
}
}

Displaying long string in pygame

If I want to display long string using pygame normally the part of string will go outside pygame screen depending on font size, so is it possible to show long string by scrolling in pygame like we see in news channel moving text on bottom of TV screen?
Yes, it is possible. Here is a draft on how it should work.
Create a class that will hold a surface where you will draw your text.
This class should also contain the current sprites as well as a list of text to be written.
Now you create these methods:
Init method
should put the first text int the current_sprite list.
Draw method
draws all sprites in the current sprite list.
An update method that does this:
For each current sprite:
moveUp
checkIf out of bounds -> if true remove from current sprites
checkIf the first sprite has crossed the y offset,
so you can spawn a new sprite at the bottom.
When you spawn a new one, you should increment the counter responsible for
remembering which text to create next.

Cocos2dx sublayers

I am developing a 2D games with cocos2dx, in which I am still very new... inside the game, there's a number of UI Elements that I'd like to group together as one (I intend to group them into CCLayers). For example a few text labels and sprites form a CStatBar which is a CCLayer. This CStatBar will then be included in the various other CCLayer
How do I do that? I created the CStatBar class and then, inside the containing class's init() function, I CStatBar::create() and call this->addChild(pStatBar) however, the statbar did not appear... is there any obvious thing that I missed? All the positions are correct. Thanks!
EDIT:
Notes:
1. ccTouchesBegan of the sublayer is called, however it is not rendered/seen
2. How do I resize the sublayer so that it only cover partial area of the parent layer? Supposedly the CStatBar should only cover 10% of the top area of the screen, not the whole screen...
inside the CParent::init() function, you can initialize the CSublayer like so:
// Create and initialize CSublayer
CSublayer* pSublayer= CSublayer::create();
float fWidth = pSublayer->getContentSize().width;
float fHeight = pSublayer->getContentSize().height;
pSublayer->setPosition(ccp(fWidth/2.0f, fHeight/2.0f));
this->addChild( pSublayer );
and your CSublayer can be defined like other CCLayer.
If you want to restrict the CSublayer to be smaller than the CParent layer, you can do so inside its init function like so:
CSublayer::init() {
// initialize the size with the size of the background sprite
CCSprite *pSpriteBackground = CCSprite::createWithSpriteFrame(
CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("background.png")
);
this->setContentSize(CCSize(pSpriteBackground->getContentSize().width, pSpriteBackground->getContentSize().height));
pSpriteBackground->setPosition(ccp(fScreenHalfWidth, fScreenHeight-(pSpriteBackground->getContentSize().height/2.0f)));
this->addChild(pSpriteBackground);
}

Movieclip stacking in Actionscript

I'm building a game of which the interface is one of the first items to load on screen. Sound button, pause and all the bits -
During the game - all manor of things are dynamically added and removed to the stage. Therefore my interface goes behind my game scene.
How do I ensure the movieclip always stays on top?
Can I override the addChild of my Document Class and every time a new child is added, I restack the interface to the top?
You can use setChildIndex to rearrange objects that are already contained within a MovieClip or Sprite. For example, if you have an object called container, which contains a redBall, blueBall and many other ball objects, you can use the following to make sure redBall is behind everything else:
container.setChildIndex(redBall, 0);
Equally you can make sure that blueBall will be displayed infront of everything else using:
container.setChildIndex(blueBall, container.numChildren-1);
addChildAt will sort you out for adding children straight into their correct position, and setChildIndex will allow you to re-position objects already placed. Hope that helps.
debu
Look into addChildAt, it allows you to specify the index that the new object should be added too.
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObjectContainer.html#addChildAt%28%29
A good strategy is to keep all interface elements in one parent Sprite/MovieClip, and all game assets in another. (With the interface one on top)
With your guys suggestion I made this, apparently, if you addChild an object already on the screen, it's simply reindex'd to the top. Does this look ok?
private var topLevelChildrenArray:Array = [];
public function addTopLevelChild(child:DisplayObject):DisplayObject
{
topLevelChildrenArray.push(child)
return this.addChildAt( child, this.numChildren - 1 );
}
override public function addChild(child:DisplayObject):DisplayObject
{
this.addChildAt(child, this.numChildren);
for each(var topChild:DisplayObject in topLevelChildrenArray)
{
super.addChild(topChild);
}
return child
}