I have a 5 frame run animation for a character on a sprite sheet. I would like to give this character multiple "upgrades", such as sunglasses, hats, etc. I don't know the "correct way" to do this, so here's what I did:
I constructed a layer to represent the sprite itself, and added the character first, then the hat on top. I have a 5 frame animation for where the hat would be on that characters head ALSO on the sprite sheet, and have it animating with the exact same parameters as the main character.
The problem is that when I try to animate the two sprites simultaneously (the character sprite and the hat sprite), they get out of sync. Sometimes it works great, but sometimes the timing is just a wee bit off, which is enough that his head will go through the top of the hat, or the hat will pop off his head every step.
My current solution is to have an update tick, and manually cycle through the frames every time some number of milliseconds has passed, which works, but I assume since I'm manually setting the frames it's using up more processor time than needed.
So what would be the "correct" way to add a hat to a sprite without having to have a "hatted sprite" series on your sprite sheet, and keep them animating together.
edit: sorry this is tagged badly, I apparently don't have a high enough reputation to tag this "cocos2dx", despite the fact that it's a cocos2dx question.
Well if i have the same situation i will go with taking different spritesheets for different animation according to upgrade of character, that way there will be no need for two different animation pluse u can adjust sprites for best effect and it won't interfare with other animation, only problem is u have to make different sprites according to upgrade.
What kind of animation are you using?
I recommend you to animate using an skeleton. This way is very easy to replace any body-part with different sprites. In TestCpp you have some examples of skeleton animation using CocoStudio (free) or Spine (paid). You even have an example of an animation where weapons are replaced.
If you need to stick to standard frame animation, you need to make sure that both animations start from the same frame when you replace anything. Otherwise, if you replace your hat when you are on a keyframe different from 0, the animation will be out of sync. One solution is to restart both animations. The other is to check the current frame of the body animation and force the hat animation to start on that frame.
OK so what I ended up doing, all the sprites that are assembling a larger character object are put together in a class, together with a float "_lastFrameUpdateTick" and int "_lastFrameNumber". in my update(dt) I have a switch case on _lastFrameNumber for each sprite so it knows if the current time tick is past the _lastFrameUpdateTick + that frame's display time, it should update all the sprites in that class to the next frame number (also allowing me to shift around sprites relative position manually, for cases where, say, a character is bouncing up and down on the back of a horse). This does require me to put copies of any hats repeatedly on the sprite sheet for every animation (though that could probably be faked with plist were one so inclined). This also has the added advantage of allowing certain frames to display slightly longer or shorter than others, allow explicit skipping around in animation sequences depending on events (if there's an earthquake and the character is on a leaned-back part of an animation it falls backwards; if it's on the leaned forward frame of an animation it falls forward), and its easy to halt animations while allowing other stuff to keep running. Most importantly is that doing it manually allows me to always know exactly what frame a sprite is on, so it can smoothly transition between animations without hiccups (I know that if i'm leaned back and want to run forward, i need to go through a leaning-forward animation first).
I'm sure there's a better way to do this, but I'm no C++ developer, and it works, so I'm moving on.
Related
after applying to a Box2D body:
b2Vec2 force = b2Vec2(velocity.x/PTM_RATIO, velocity.y/PTM_RATIO);
_body->ApplyLinearImpulse(force,_body->GetPosition());
I'm trying as in many game (like doodle jump) to stop moving the hero body once it reaches a certain distance from the top of the screen and start scrolling the stage so that we feel the hero is still climbing higher. For this I need to move the hero in the first place and then move the stage.
How can this be achieved correctly ? Any idea?
Thats quite an odd thinking to solve the problem. You never stop the character or any of the environment objects. Let them behave as they are intended by box2d. You have everything added to one root node of some sort, your environment and the character. What you do is create some sort of a "camera controller" and you give the characters CCSprite, or the wrapping objects if you have one, as a target. In the update function that you call each frame you change the root nodes position in a way that it centres the screen on the character. You can implement follow delays, smooth scrolling and other nice features as you need them.
Unfortunately I don't use Cocos2d at all, so I can't give you a sample code. The given solution will work for Cocos2d as it is not far of the engine I use at my work place.
I am attempting to animate a model similar to the ones here: http://wollay.blogspot.com/2012/09/new-cube-world-video.html. It is made up of cubes, and divided into several parts: the head, body, arms and legs. I have no experience with animation. How would I create a walking animation similar to the ones seen in that video using opengl?
If you want to do "simple" animation (like very rigid). You can simply just rotate the legs and arms about the connection point to the body over time by simply varying the speed of movement. For instance in your timer function or idle function you check how much time has elapsed and set the rotation of the part to some number based on that until a certain point where it swings back the other way of course in an arc.
If you want more complicated things you are better off animating and creating the models in a 3d modeling tool that allows you to create animations as well. Trying to program more complicated animations may be difficult.
For starters you can always just do the simple rigid swing animations and maybe try to add some more movement programatically such as some side to side sway of arms which is simple if you already did the back and forth swinging and some basic idle animations.
So, what we need to implement is a tree-menu with several nodes (up to hundreds). A node can have children and then can be expanded/collapsed. There is also a background lightning by mouse over and a background lightning by mouse selection.
Each node has a box, an icon and a text, which can be very large, occupying the whole width screen.
This is an example of an already working solution:
Basically I am:
rendering text a first time, just to get the length of an possible background highlight
rendering boxes and icon-textures (yeah I know, they are upside down at the moment)
rendering text a second time, first all the bold one and then all the normal one
This solution actually has a relative acceptable performance impact.
Then we tried another way, that is using the g Graphic java by drawing the tree menu and returning it as a bufferedImage to create at the end a big texture render it. All this obviously is done at every node collapse/expande and at each mouse movement.
This performed much better, but Java seems to have some big troubles handling the old bufferedImages. Indeed ram consumption increases constantly and forcing a garbage collection improves only slightly memory increasing, by slowing down it, but still...
Moreover performances fall, since the garbage collector is called every time and does not seem light at all.
So what I am going to ask you is: which is the best strategy for my needing?
Would be also maybe feasible to render each node on a different texture (actually three: one normal, one with a light background for mouse over and a last one with a normal background for mouse selection) and then at each display() just combine all these textures with the current tree-menu state?
For the Java-approach: If the BufferedImage hasn't changed in size (the width/height of your tree control), can't you reuse it to avoid garbage collection?
For the GL-approach, make sure you minimize texture switches. How do you render text? You can have a single large texture that contains all the normal and bold letters and just use different texture coordinates for each letter.
I'm building a cocos2d game where I use two background sprites, actually one is a sprite, the other one is a CCMask that is used to make holes into the other background, but the performance problem is the same even when using 2 regular background sprites on top of each other.
When I use one background sprite, my FPS is around 60 all the time, when I use two background sprites the FPS drops to 30 every time. I've googled around, tried different solutions including reading sprites from a sprite frame cash instead of from a file, unfortunately the result is the same.
I just can't figure out why this is happening. Does any one here have any idea why this is happening and how to get around it?
On older devices (1st & 2nd generation, ie iPhone 3G) this can easily happen since they have terrible fillrates.
If possible try to SpriteBatch the two background images. You need to add both to a texture atlas, for example with TexturePacker. Sprite batching is particularly effective if the sprites are large.
Also, just in case: don't test performance in the Simulator. Simulator performance has no relation to actual device performance whatsoever.
When I drag an object in my game, the object is never directly under the finger. There us this lag / delay that I cannot get rid of. It follows my finger instead of being directly underneath it. You can try in the Testbed as well. Trying moving an object really fast and the object is never underneath the mouse/finger
Is this a weakness in box2d? Or am I missing something obvious ?
Thanks in advance
That's because mouseJoint is similar to distantJoint (spring). There is a maxForce parameter you can specify to minimize the delay - make the spring more hard.
EDIT:
Also you can move your object directly specifying it's position to your finger position. But if this object will collide with something it will provide non-physical behavior because the velocity of the body will be zero.
So to move it correctly (if there will be collisions) you should specify it's velocity or acceleration (as mouse joint does). But to evaluate your finger velocity you will need some time and delay will remain.
Most of it has to do with latency in the hardware. If your timings are completely perfect, their will be 16ms of lag caused by the iPhone's GPU, ~20ms of lag from the touchscreen, and then how ever long the processing takes for your scene. So those add up to anywhere between 36-70ms of lag. Also, there is a small amount of damping applied in box2d on the mouse joint, for stability of the physics simulation.