Adding CCLabelBMFont to a sprite in cocos2d-x - cocos2d-x

Lately i have encountered a problem that my program is crashing when i am trying to add BMLabelFont to a sprite
CCString *coinLabelString = CCString::createWithFormat("%f",health);
healthLabel = CCLabelBMFont::create(coinLabelString->getCString(), kFontChubby, 0);
healthLabel->setPosition(ccp(winSize.width * 0.525, winSize.height * 0.89));
healthLabel->setAnchorPoint(ccp(0, 0.5));
sprite->addChild(healthLabel, 1);
Like this... I am adding the same to game layer and its working.... trying to add it to sprite it crashes with this error
Cocos2d: createFontChars : cocos2d::CCLabelBMFont: Attempted to use character not defined in this bitmap: 30584
Cocos2d: createFontChars : cocos2d::CCLabelBMFont: Attempted to use character not defined in this bitmap: 4450
this sprite is added to a CCSpriteBatchnode... can it create a problem... anySolution to this problem....
Thanks in advance

Related

Creating sprite from texture atlas

In my libGdx project,I created a sprite from texture atlas,using createSprite().
I want to implement the sprite as rotated.
How can I do it?Here is my Code:
reelSprite = atlas.createSprite("reel");
Inside render():
for (Wall lWalls : leftWalls){
reelSprite.setOrigin(lWalls.getX(), lWalls.getY());
reelSprite.setRotation(180);
batch.draw(reelSprite, lWalls.getX(), lWalls.getY());
}
This code is not working.Please tell me what wrong I did.
You should use setRotation before you draw the sprite:
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Sprite.html#setRotation-float-
And even before that set rotation point:
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Sprite.html#setOrigin-float-float-
float rotate = 0;
rotate += (sprite.getRotation() - 40) * Gdx.graphics.getDeltaTime();
if(Math.abs(rotate) > 10) // change the number to set the rotation power cap
{
rotate = -10;
}
sprite.rotate(rotate);
I changed the code like this...
for (Wall lWalls : leftWalls){
reelSprite1.setPosition(lWalls.getX(), lWalls.getY());
reelSprite1.setOrigin(reelSprite1.getWidth()/2,reelSprite1.getHeight()/2);
reelSprite1.setRotation(180);
reelSprite1.draw(batch);
then it worked.

Sprite visibility problems after using low values of alpha

The plan was very simple: Using MouseEvent.CLICK to hide/show a Sprite. The first click should make it disappear, the second make it visible again.
What actually happened was really odd, as the Sprite didn't become visible when alpha was set to 1 (unless I zoom in or open the Settings menu). Here's an example: http://www.fastswf.com/8BuuY14
private function doStuff(e:MouseEvent):void {
(e.target.alpha == 1) ? e.target.alpha = 0 : e.target.alpha = 1;
}
//Sprite on the left
var outter:Sprite = new Sprite(); //Container sprite (gray background)
outter.x = outter.y = 20;
outter.opaqueBackground = 0xCCCCCC;
outter.addEventListener(MouseEvent.CLICK, doStuff);
var inner:Sprite = new Sprite(); //Interactive child (red square)
inner.graphics.beginFill(0xFF0000);
inner.graphics.drawRect(0, 0, 50, 50);
var speck:Shape = new Shape(); //Reference child (tiny black square)
speck.graphics.beginFill(0x000000);
speck.graphics.drawRect(50, 50, 5, 5);
outter.addChild(inner);
outter.addChild(speck);
addChild(outter);
//Sprite on the right
var cont:Sprite = new Sprite();
cont.x = 100; cont.y = 20;
cont.graphics.beginFill(0xFF0000);
cont.graphics.drawRect(0, 0, 50, 50);
cont.addEventListener(MouseEvent.CLICK, doStuff);
addChild(cont);
I did manage to make it work, by using alpha values equal to or larger than 0.0078125 (in true alpha value), but not 0. Why is this happening?
[EDIT]
Since I established the error could be caused by my IDE, I requested help also at the FlashDevelop community (see comments for link).
After many tests ( on windows 7 with firefox, chrome and IE ), I can confirm that the "problem" ( the abnormal behavior, at least ) is linked to these factors :
Using a MovieClip, Sprite or a Shape ( and some other objects ).
The wmode ( when used ) : direct and gpu.
The Flash Player version : ActiveX, NPAPI and Standalone for the version 14 and up.
I think that's like the Flash Player is "ignoring" to render the object may be for a performance issue that's doing that ( not rendering the object when its alpha is 0 from the 1st time ), because sometimes when I used a Button or CheckBox, ... and I rollover it, a Event.RENDER event is fired on the object and the code is working one time. I saw also that the code is working when executing it for two objects in the same time ...
So to avoid that behavior, you can use the cacheAsBitmap property :
object.cacheAsBitmap = true;
You can also using a ColorTransform object when setting the alpha :
object.transform.colorTransform = new ColorTransform(1, 1, 1, (object.alpha == 1 ? 0 : 1), 0, 0, 0, 1);
Or simply, avoid to use the direct or gpu wmodes ...
Hope that can help.

Moving 3D charracter - I don't want any physics, expected of collisions, and gravity

I am working on a game. I constructed my player as here: (I am using a gravity on my world)
private ArrayMap<String, GameObject.Constructor> constructors = new ArrayMap<String, GameObject.Constructor>(String.class, GameObject.Constructor.class);
private ArrayList<GameObject> instances = new ArrayList<GameObject>();
assets.load("hand.obj", Model.class);
...
model = assets.get("hand.obj", Model.class);
constructors.put("hand", new GameObject.Constructor(model, new btBoxShape(new Vector3(2.5f, 7.5f, 2.5f)), 1f));
...
hand = constructors.get("hand").construct(); // that construct method returns me model, shape and constructions.. the GameObject extends ModelInstance, so i can use it like a modelinstance
hand.transform.setToTranslation(x, y, z);
hand.body.proceedToTransform(hand.transform);
hand.body.setUserValue(instances.size());
hand.body.setCollisionFlags(hand.body.getCollisionFlags()| btCollisionObject.CollisionFlags.CF_CUSTOM_MATERIAL_CALLBACK);
world.addRigidBody(hand.body);
hand.body.setContactCallbackFlag(OBJECT_FLAG);
hand.body.setContactCallbackFilter(OBJECT_FLAG);
Then, in render method I am moving it:
if (!hand.body.isActive()) hand.body.activate();
if (Gdx.input.isKeyPressed(Keys.W)){
hand.body.translate(new Vector3(0,0,-1));
}
else if (Gdx.input.isKeyPressed(Keys.S)) {
hand.body.translate(new Vector3(0,0,+1));
}
That's nice! The moving now works good, when I am moving at the flat ground. Whenever there is an object before me, it is not as expected. Because my player shape is biger than
object shape (which is 2.5f, 2.5f, 2.5f), it kind of falls on it. So I would like to set the rotation to be still the same, so the object will not be rotating (so it will not "fall" on the object before). And so I tried to do it, and I failed. Because there are functions like rotate, and I want to something like setRotation
. And so, there is a setToRotation, but you can not pass there a Quaternion.
I need help. I tried to use a btKinematicCharacterController but it was bad. The ghostObject every time falled through object, but the objects got a collision from him.
and so I want to create a player movment, like in games like Wow, minecraft, and so on.
I looked at the btKinematicCharacterController again. The reason why my ghostobject falled through the ground was. Generally, I don't know the reason: D probably I was using another broadphase for ghost, that for world. This line fixes it: characterController.setUseGhostSweepTest(false);
and I am getting another problem, when I am walking on my ground (a lot of objects), the character is getting to lesser Y position. I don't know why.
Here is my construction:
btPairCachingGhostObject ghostObject;
btConvexShape ghostShape;
btKinematicCharacterController characterController;
Vector3 characterDirection = new Vector3();
Vector3 walkDirection = new Vector3();
...
ghostObject = new btPairCachingGhostObject();
ghostObject.setWorldTransform(hand.transform);
ghostShape = new btCapsuleShape(5f, 0.5f);
ghostObject.setCollisionShape(ghostShape);
ghostObject.setCollisionFlags(btCollisionObject.CollisionFlags.CF_CHARACTER_OBJECT);
characterController = new btKinematicCharacterController(ghostObject, ghostShape, .00001f);
// And add it to the physics world
characterController.setUseGhostSweepTest(false);
world.addCollisionObject(ghostObject,
(short)btBroadphaseProxy.CollisionFilterGroups.CharacterFilter,
(short)(btBroadphaseProxy.CollisionFilterGroups.StaticFilter | btBroadphaseProxy.CollisionFilterGroups.DefaultFilter));
world.addAction(characterController);
... (in render - moving)
if (!load)
{
if (Gdx.input.isKeyPressed(Keys.LEFT)) {
hand.transform.rotate(0, 1, 0, 5f);
ghostObject.setWorldTransform(hand.transform);
}
if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
hand.transform.rotate(0, 1, 0, -5f);
ghostObject.setWorldTransform(hand.transform);
}
// Fetch which direction the character is facing now
characterDirection.set(-1,0,0).rot(hand.transform).nor();
// Set the walking direction accordingly (either forward or backward)
walkDirection.set(0,0,0);
if (Gdx.input.isKeyPressed(Keys.UP))
walkDirection.add(characterDirection);
if (Gdx.input.isKeyPressed(Keys.DOWN))
walkDirection.add(-characterDirection.x, -characterDirection.y, -characterDirection.z);
walkDirection.scl(4f * Gdx.graphics.getDeltaTime());
// And update the character controller
characterController.setWalkDirection(walkDirection);
// And fetch the new transformation of the character (this will make the model be rendered correctly)
}
world.stepSimulation(delta, 5, 1f/60f);
if (!load)
ghostObject.getWorldTransform(hand.transform);
How to fix this?
I set up the debugDrawer, so i was able to see the shapes of the bullet objects.. and my problem was that: the ghostObject(charController) was pushing my objects down.. Although my objects were static. So i set the mass of the objects to 0 and problem is fixed. But I still dont know, how it could push static objects. But i dont care. :)
EDIT: i will accept this answer in 2 hours, because now i cant.

Try to add VBO in Slick2D

I'm trying to add VBO in slick2D. All I find on the web is how to initialize VBO in a 3D context. Anyone knows how to do it in 2D ?
My actual test (make 4 square in slick context) make this (i add corrds in black) :
(source: canardpc.com)
.
Below my init (in the init method of my GameState) :
// set up OpenGL
GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
GL11.glMaterial(GL11.GL_FRONT, GL11.GL_SPECULAR, floatBuffer(1.0f, 1.0f, 1.0f, 1.0f));
GL11.glMaterialf(GL11.GL_FRONT, GL11.GL_SHININESS, 25.0f);
// set up the camera
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
// create our vertex buffer objects
IntBuffer buffer = BufferUtils.createIntBuffer(1);
GL15.glGenBuffers(buffer);
int vertex_buffer_id = buffer.get(0);
FloatBuffer vertex_buffer_data = BufferUtils.createFloatBuffer(vertex_data_array.length);
vertex_buffer_data.put(vertex_data_array);
vertex_buffer_data.rewind();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertex_buffer_id);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertex_buffer_data, GL15.GL_STATIC_DRAW);
And the render (in the render method of game state) :
g.setDrawMode(Graphics.MODE_ALPHA_BLEND) ;
// perform rotation transformations
GL11.glPushMatrix();
// render the cube
GL11.glVertexPointer(3, GL11.GL_FLOAT, 28, 0);
GL11.glColorPointer(4, GL11.GL_FLOAT, 28, 12);
GL11.glDrawArrays(GL11.GL_QUADS, 0, vertex_data_array.length / 7);
// restore the matrix to pre-transformation values
GL11.glPopMatrix();
I think something wrong because all other render disappear (text and sprites) and coords are not window size anymore.
edit : I try something like this GL11.glOrtho(0,800,600,0,-1,1); with strange result
Thanks
I resolv the issue by adding GL11.glOrtho(0,800,600,0,-1,1); and disabling glEnableClientState (glDisableClientState).
But I will finally move to libgdx framework whoes do that natively.

In AS3, how to update a portion of a bitmap with a Pixelbender instead of the whole bitmap?

In pure AS3, I have a pixelbender and a large bitmap. The pixelbender is configurable with a distance parameter to affect only a small area of the bitmap. The problem is that the pixelbender is executing over the whole bitmap. What would be the best way to update only the effected region of the bitmap?
Given this config:
shader.data.image.input = referenceBitmap.bitmapData; // 300x200
shader.data.position = [150,100];
shader.data.distance = [20];
The following does not work:
new ShaderJob(shader,
bitmap.bitmapData.getPixels(
new Rectangle(particle.x -10,
particle.y -10,
20,
20))).start();
I could make a temporary array to hold the computed values and then copy them back into the bitmapData array. Although I would like for the shader to update the bitmapData pixels directly and only on the affected area.
The following works, but the shader runs on the whole 300x200 bitmap:
new ShaderJob(shader, bitmap.bitmapData).start();
Any suggestions?
EDIT: a filter will not work as there are 3 input images
BitmapData allows you to use a function called applyfilter.
Instead of trying to use a bitmapdata with a shaderJob you could alternately use the shader as a shaderFilter and apply the shaderfilter on the bitmapdata.
shader = new Shader(fr.data);
shaderFilter = new ShaderFilter();
shaderFilter.shader = shader;
Once you have your shaderFilter you could use applyFilter on your bitmapData. Something like :
referenceBitmap.bitmapData.applyFilter(bitmap.bitmapData,new Rectangle(particle.x -10,
particle.y -10,
20,
20),new Point(0,0),shaderFilter);
Hope this helps.