I am moving my sprite using CCBezierTo:
id streakAcross = [CCBezierTo actionWithDuration:myDuration bezier:bezier];
I would like to modulate (sinusoidal?) the sprite along the path or rather introduce a smooth wobble that moves the sprite along either side of the bezier path. I think I can spawn a ccmoveby action simultaneously, but have not gotten my head around it.
I have solved this by embedding the sprite inside a ccnode. The sprite is animated up and down (forever) using CCMoveBy-s relative to it's parent. Then I animate the parent node along the bezier path. I added an ease out action to smooth things out.
Related
I'm recently trying to build a program by myself, and for this reason I need to make a Sprite bigger when the MouseCursor is over it. Is solved it by checking for Mouse movement and then checking if ths mouse is over the Sprite. I increase the size of the sprite by using the setScale(); function. The problem then was, that the Sprite has been enlarged, but the center of the Sprite has been moved. I tried to set the Origin to the center of the Sprite, but now, the texture is at an other place than the Sprite.
The Texture is 500x500px large, so I set the origin to 250, 250. The Texture of the sprite moved to the right spot, since the origin is at an other place. The Position is "50, 250", so the texture is slightly off the screen. When I set a new position, the texture moves, but the sprite also. So texture and sprite don't match anymore.
Does anyone know how to fix the problem?
In a game I am programming in Cocos2D and Box2D, I have a sprite that is in the shape of a hill, and I am trying to attach a body to it so it can respond to physics. My idea is to use a rotated rectangle shape so that it aligns with the slope, but I need to be able to move the sprite so that the body stays appropriately lined up. I can make the body and sprite work together in the case of a circle, but I can't figure out how to make it work for the case of the rotated rectangle. Could someone explain the code to do this?
Yes I agree to user1634707. CocosBuilder will be one potential choice.
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.
I'm using Cocos2D with Box2D, and moving from bodies with a single fixture to bodies with multiple fixtures, each with a corresponding sprite. By using
body->GetTranform().p.x + shape->m_centroid.x
body->GetTranform().p.y + shape->m_centroid.y
body->GetTransform().q.GetAngle()
I am able to position the sprites properly, until the body is rotated. At that point, all goes awry, with each sprite spinning in relation to its siblings, and the whole group spinning off around what I think is a (0, 0) body anchor point.
My guess is that Cocos2D/Box2D has a simple, built-in method for performing the necessary transform, but I haven't been able to find it documented. What is the best way to position my sprites correctly?
Try rotating sprites with respect to body center in update tick function.
I finally located the transform function I was looking for.
b2Vec2 ptWorld =body->GetWorldPoint(shape->m_centroid);
transforms the fixture shape's coordinates, measured relative to the body's origin, into world coordinates. After converting from meters to pixels, ptWorld's x and y coordinates can be used to set the sprite's location.
Currently the setanchor function only sets the anchor within the sprite frame. Is there any (easier) wway to change the anchor point to a coordinate on screen space? My purpose is to allow the sprite to rotate around a circle.
TIA for any suggestions made.
Use the Node hierarchy to your advantage. Create a new Node (which won't be visible unless you want it to be) and add your sprite to the node as children. Position the sprite child somewhat away by giving it a position of 100, 100 for example.
Now if you rotate the node using the rotation property, instead of the sprite, the sprite should rotate with the node, making it appear as if it goes around in a circle. The node itself will be the centerpoint of the rotation.
I've added this Q&A to my cocos2d FAQ:
http://www.learn-cocos2d.com/knowledge-base/cocos2d-iphone-faq/learn-cocos2d-public-content/manual/cocos2d-general/14826-how-to-rotate-a-sprite-in-a-circular-motion