Making a Drawable layer in cocos2d - ios

I want to make sort of a chalk board for part of my app, and I was wondering how to accomplish this?
I was thinking I could create a sprite and have it's image set to something very small (maybe a small point), and then add a new instance of that sprite everywhere the user touches to simulate a draw event. Something like [self addChild:someSprite]; for each touch location.
But it seems like that would be extremely memory inefficient. There has to be a better way than that, Maybe drawing actual lines? I'm probably overlooking some method.
Thanks for any help.

You need to use CCRenderTexture for chalk board paintings. Check this article & project for a drawing example.

Your variant isn't such "memory inefficient" as you think. No matter how much sprites will you create with the same texture, your texture will be placed to the memory only once. And all the sprites will use pointer to it. Just one thing to prevent many unnessesary calls is to use CCBatchNode. It will draw all it's children with single draw call. Without using it, draw will be called on every children.

Related

I need help to draw an icosahedron 3D object in an UIKit app

Although I am quite experienced with most frameworks in iOS, I have no clue when it comes to 3D modelling. I even worked with SpriteKit, but never with something like SceneKit.
Now a customer wants a very ambitious menu involving a 3D object, an 'icosahedron' to be exact. I want it to look something like this:
So I just want to draw the lines, and grey out the 'see-through' lines on the back. Eventually I want the user to be able to freely rotate the object in 3D.
I already found this question with an example project attached, but this just draws a simple cube: Stroke Width with a SceneKit line primitive type
I have no clue how to approach a more complex shape.
Any help in the right direction would be appreciated! I don't even need to use SceneKit, but it seemed like the best approach to me. Any other suggestions are welcome.
to build an icosahedron you can use SCNSphere and set its geodesic property to YES.
Using shader modifiers to draw the wireframe (as described in Stroke Width with a SceneKit line primitive type) is a good idea.
But in your case lines are not always plain or dotted — it depends on the orientation of the icosahedron. To solve that you can rely on gl_FrontFacing to determine whether the edge belongs to a front-facing or back-facing triangle.

Drawing lines in cocos2d

I'm trying to draw lines in Cocos2d using touches.
I had a system where it would just add a small sprite where you touched, but it's working terribly. So I've been trying to find a way to draw actual lines using a method like ccDrawLine, but every tutorial I find seems to leave out something, and I just can't figure it out.
I've found this tutorial, Drawing line on touches moved in COCOS2D but I don't understand a few things about that.
It seems to reference the same variable from two different files, so I don't understand how it's doing that. (The naughtyTouchArray variable)
I can't find a complete guide on drawing lines, so sorry for the codeless question, but I'm getting frustrated.
Thanks.
The answer you've linked in your question provides good solution to your problem. There is no "two different files". Just two different methods of one layer. One method (ccTouchesMoved:withEvent:) handles touches and fill the array of points to be connected to each other one-by-one with lines. From cocos2d documentation, all drawing must be placed in the draw method of the node. So, another (draw) method just draws lines according to the given array. Cocos2d is based on OpenGL and it fully redraws scene every tick, so you cannot just draw new line. You had to draw all of them.
Or any other node can draw your array in it's draw method, so you can simply pass stored array of points from the layer, that detects touches, to this node.

Creature animation

I am looking into making an iOS app that has little creatures. I plan on having these creatures grow and change shapes based on user interaction. So the creatures could end up looking very different based off what the user does.
My problem is animating these creatures. I have dealt with simple animations in the past with cocos2d, but nothing like this.
How can I animate these creatures being different sizes and shapes without having my graphic designer draw every possible image that could be used. In the game spore a user can create an animal of whatever shape or size they want and these animals animate. My question is how can I do something similar in 2d? I know this can't be a simple answer, but a point in the right direction is all I am looking for.
You could use some CGAffineTransforms to scale your drawing and custom filters with Core Image maybe to change the color.

Commiting drawing from touch events rather than large array

A small portion of my app contains a view were people can take brief notes by drawing on the screen. Currently, I do this by saving the touch points into an array then connecting the dots in drawRect. I know that this isn't the best way to do this but this is only a small portion of my app.
The problem is that after awhile the array of touch points get's pretty large and performance bogs down.
Can someone suggest a better way to do this rather than a giant connect-the-dot's game? I am thinking that at each touchesEnded event I should somehow commit what was drawn during that touch to a separate drawing, then clear out the array but I'm not sure how to do that?
Keep a CGMutablePath (or UIBezierPath) property and append to it. Constructing large paths is very expensive, so you should definitely not do that inside of drawRect:. If you do have to construct paths very often, it is generally cheaper to have a few smaller paths than one massive path.
You can also just draw each new path into a persistent CGLayer and draw it in drawRect:. This is less flexible, but has a fixed memory and processing requirement. You can achieve similar things by updating the contents of a CALayer.

XNA beginner question about draw method

I understand that I have to draw everything in draw(), and it's looping continuously.
But I don't want to draw texture again and again, for example I want to create a texture, draw something to texture (not spritebatch). than I will only draw that texture in draw().
Is it possible?
What can I use?
You have to draw again and again, as in short, if you dont it wont show. A wise man once wrote in a windows development book
Ask not why the text on your windows has to be constantly drawn, ask why it never used to be in DOS/Unix command line.
If something is placed over the area you're drawing too, and you dont redraw it, it just simply wont be there. You need to keep drawing it for it to be sustained on screen. Its done very quickly and wont hurt anything (especially if you're thinking in terms of background)
Not drawing it again is a performance optimisation. You should only do that if you really need to.
If you do need to do this, create a render target, draw your scene to the render target, and then draw your render target to the screen each frame (using SpriteBatch makes this easy) instead of your scene.
Take a look at this question about caching drawing using render targets.

Resources