This is my very first question in Stack Overflow.
I am playing around with the skeletal animation with Cocos2d. By using TheDamArmada / Flash2Cocos2D library,I can init a FTCCharacter which is inherited from CCSprite. I try to flip the whole character by using myRobot.flipX = YES, but it is not working.
Any suggestion?
Here's an inline link to Flash2Cocos2D.
Thanks.
From Cocos2D Source (CCSprite.h:121)
/** whether or not the sprite is flipped horizontally.
It only flips the texture of the sprite, and not the texture of the sprite's children.
Also, flipping the texture doesn't alter the anchorPoint.
If you want to flip the anchorPoint too, and/or to flip the children too use:
sprite.scaleX *= -1;
*/
#property (nonatomic,readwrite) BOOL flipX;
So it is expected that children of FTCCharacter is not flipped.
myRobot.scaleX *= -1; should work as suggested by Cocos2D.
Related
I have a sprite, a square, just for orthogonal projection. Now I want to project it in a very basic, simple isometric way. (I know this might not be pretty, but I just want to figure this out)
Given my square, I rotate it 45 degrees. Now if I understand correctly, I should still divide my height by 2. This has been impossible for me in SFML. There is a scale function but if I scale with a factor 0.5 in the y-axis direction, my cube just gets stretched, instead of a diamond shape. It looks as though SFML transforms the sprite according to it's own relative axes (that were rotated before..).
Since you cannot access the height of a sprite, I was wondering if this was even possible?
Can I convert a square sprite to a diamond shape in SFML?
Using a sf::RenderTexture is an option (see other answer). Another option is to fiddle with the sf::View. Double the view's height, and adjust coordinates. It would go something like this:
my_sprite.setRotation(45.f);
//adjust the position for new screen coordinates (once)
my_sprite.setPosition(my_sprite.getPosition().x, my_sprite.getPosition().y * 2);
//...
//when drawing:
sf::View v = my_render_window.getDefaultView();
v.setSize(v.getSize().x, v.getSize().y * 2);
v.setCenter(v.getSize() *.5f);
my_render_window.setView(v);
my_render_window.draw(my_sprite);
my_render_window.setView(my_render_window.getDefaultView());
Rotate your sprite as you are doing now. Render it to an sf::RenderTexture. Use the member function getTexture, and make a new sprite from it, or reuse the old sprite. Scale the sprite along the y-axis. Draw it to the render window.
Some math on your part may be required in order to set the RenderTexture to the right size and to draw the original sprite in the correct location on it.
original_sprite.setRotation(45);
sf::RenderTexture rt;
rt.create(FigureOutWidth(),FigureOutHeight());
original_sprite.setPosition(MoreMathHere());
rt.draw(original_sprite);
sf::Sprite new_sprite(rt.getTexture());
new_sprite.setScale(1.0,0.5);
It should go without saying, but do this once in initialization, not every frame.
Suppose i have drawn a triangle,a cube,a square using draw_triangle,draw_cube and draw_square function respectively.How can I delete a cube using keypress func.?..If i use glClear(GL_COLOR_BUFFER_BIT) entire screen will be erased..How to delete a specific polygon.?
How to delete a specific polygon.?
Don't render it in next frame.
Once you draw a polygon, there is no polygon, just a bunch of pixels in color buffer + values in depth buffer. So you can't "delete" it, and you can't restore previous state of color/depth buffer. So clear screen, redraw the scene without polygon you didn't want.
If you want to clear only a specific part of the screen or any specific image, then redisplay it by using a keyboard interrupt wihout clearing the whole screen.
I think the best solution is to have a boolean variable which indicates whether your object must be displayed or not. Thus, only when a key is pressed, you clear the screen and redraw the entire scene.
maybe something like that:
Render()
{
clear_screen();
setup_camera_and_other_scene_states();
if (cube) drawCube();
if (sphere) drawSphere();
swap_buffers():
}
keyPress()
{
if (presses_some_key) cube = !cube;
}
I was trying to use CCRenderTexture for pixel perfect collision detection, as outlined in this forum posting:
http://www.cocos2d-iphone.org/forum/topic/18522/page/2
The code "as is" works, and I have integrated it with my project
But I am having trouble doing some of the other things discussed:
If I create the renderTexture to be any size less than the screen size, the collision detection doesn't work properly - It seems to show collisions when the sprites are close (<15px) to each other but not actually colliding.
Also I have trouble changing the location of the render texture. Regardless of the position I specify, it seems to go from bottom left (0,0) till the width & height specified. I followed this post:
http://www.cocos2d-iphone.org/forum/topic/18796
But it doesn't solve my problem. I still get bad collisions like specified above. Also, the first post I mentioned on the list contains comments by many users who have resized their textures to 10x10, and repositioned them off screen.
Does anyone have any sample code, so I can see what I am doing wrong? I just use the boilerplate code:
CCRenderTexture* _rt = [CCRenderTexture renderTextureWithWidth:winSize.width height:winSize.height];
_rt.position = CGPointMake(winSize.width*0.5f, winSize.height*0.5f);
[[RIGameScene sharedGameScene]addChild:_rt];
_rt.visible = YES;
I use cocos2d-iphone 1.0.1
You need to move the sprites you intend to draw into the region of the renderTexture before calling draw or visit. Moving the renderTexture does not change the position of _rt.sprite.
The intersection rectangle must be in the region of the renderTexture, otherwise you get inaccurate collisions.
It seems that you cannot change the position of _rt.sprite.
The solution that I use is to determine the origin (x,y) of the intersection box, and offset both the colliding sprites by that much. This will ensure that the intersection rectangle will have its origin at 0,0. Then I calculate the intersection rectangle again (after ensuring the origin of the intersection rect is 0,0) . Then I follow the instructions in the forum posting.
When determining the dimensions of the render texture, I ensure that they are at least as large as the intersection rectangle, and I ensure that the intersection rectangle is fully inside the render texture. This way there are accurate collisions. If even part of the intersection box is outside the render texture i get inaccurate collisions, so before drawing into the render texture, make sure you move the sprites you intend to visit so that the intersection box is entirely within the render texture.
Remember to move the sprites back after you're done. :)
How will I show 2 sprites on same layer. Presently sprites with horizontal image is coming behind the other sprites. Horizontal image sprites drawing in CCRibbon class.
Well, you could assign them a z order. This is done by using code like this:
[self addChild: MySprite z:1]
The higher the z order, the highest up the sprite will be.
I am doing an application where I need to get the alpha value at the touched position on a sprite. Thanks in Advance
I use this code to retrieve the alpha value:
GLubyte pColor[4];
CGPoint newpoint = (...your screen point in GL coordinates);
glReadPixels(newpoint.x,newpoint.y,1,1,GL_RGBA,GL_UNSIGNED_BYTE,&pColor[3]);
pColor[3] then contains your alpha value.
You probably will have to combine this with code to check whether your position is inside the sprite's bounding box. Make sure you get the right coordinates.
Probably it what you need sprite.alpha.