Cocos2d-iphone animating transition from one frame to another - objective-c

Is it possible to make animation for one sprite, that one frame fades out so another one is visible? It's like creating two sprites on the same position and the one that is on top looses its opacity. I would like rather to operate on one sprite with two frames than with two sprites.
Thanks for any advice!

A sprite can only display one "frame". For your requirements you have to use two sprites, there's no way around that. After all you're displaying two images at the same time, one fading out, the other fading in.

Related

Checking an SKNode is onscreen and visible

Is there a way to check when a SKSpriteNode / SKNode is onscreen (i.e. visible) I have a large scrolling background where I am spawning mobs, but I want to limit their animations and sounds if they are not visible. Is there a way to do this, I could write something in the update loop but I wanted to see if there was anything I could test before I start querying mob positions every frame?
I think you are doing this wrong. You should stick to the MVC pattern. As such, you only have data/points that are moving in a large area and in update method, if any data is within screen area, only would you draw them. If it is out, remove them.

Too many UIViews causes lag

I am creating an app for practice that is a simple drawing app. The user drags his/her finger along the screen and it colors in a 100px x 100px square.
I currently achieve this by creating a new colored UIView where the user taps, and that is working. But, after a little time coloring in, there is substantial lag, which I believe is down to there being too many UIViews as a subview of the main view.
How can I, and others who similarly create UIViews on dragging a finger reduce the lag to none at all, no matter how many UIViews there are. I also think that perhaps this is an impossible task, so how else can someone like me color a cube of the size stated above in the main view on a finger dragged along the screen?
I know that this may seem like a specific question, but I believe that it could help others understand how to reduce lag if there are a very large amount of UIViews where a less performance reducing option is available.
One approach is to draw each square into an image and display that image, rather than keeping around an UIView for each square.
If your drawing is simple enough, though, you can use OpenGL to do this, which is much faster. You should look at Apple's GL Paint Sample Code which shows how to do this in OpenGL.
If your drawing is too complex for OpenGL, you could create, for example, a CGBitmapContext, and draw each square into that context when the user drags their finger. Whenever you draw a new square into that bitmap, you can turn the bitmap into an image (via CGBitmapConxtextCreateImage) and display that image an a UIImageView.
There are two things that come to my mind:
1- Use Instruments tool to check if you are leaking any memory
2- If you are just coloring the views than instead of creating images for each of them, either set the background color property of UIView or override the drawRect method to do custom drawing
I think what you are looking for is the drawRect: method of UIView. You could create your custom UIView (you propably have that already) and override the drawRect method and do your drawing there! You will have to save your drawings in an array or another container and call the setNeedsDisplay method whenever the array content is changed.

Scrolling Scene in cocos2d

I have a sprite which moves across the screen.so i want to switch between the scenes.how can i do that.i am not interested in using Tile Map.I want just like angry birds scene scrolling.
Any Idea,
Thanks
If i understand what your asking you could just add everything to a cclayer and have everything as a child of it and move the full layer left or right to scroll
As glogic says, create a CCLayer and then add a CCSprite to it with an image much larger than the screen size.
Now in your touch handling code, do something like this...
OnTouchBegin - store position of the touch
OnTouchMoved - calculate distance from stored position where touch started and move the whole layer by that much.
OnTouchEnd - If your sprite has move too far and you have gone off the edge of the sprite, slide it back to the edge

object position relative to a different subview

I am thinking about creating a Game Board view with an array of subviews to form slots for tiles. I plan to have tiles that I can drop onto the board and have them snap into position. The part I am unclear about is when I drop my tile and the touchesEnded event fires, what is the best way to loop through the subviews of my Game Board to see which slot I am over so I can have the tile snap into proper position? Or is there a better way to keep track of all the "slot" positions?
I really don't want to have to hardcode every cell position and then keep track of it if the Game Board ever gets shifted in my view controller.
Check out hitTest:withEvent: and pointInside:withEvent:.
ditto imaginaryboy. you don't need to loop through anything. let UIKit do the heavy work for you. Compute the center point of the piece that was dropped on your board and the use hitTest:withEvent: on the view containing your board.

Animate the drawing of a line (Quartz 2D?)

How would you go about animating the drawing of a line in a UIView on the iPhone? Is this possible? It can be drawn but can it easily be animated so it appears to be hand drawn?
There's no built-in way to do this no. You'd have to redraw the line repeatedly, interpolating between the start and end points using a timer callback to invalidate the view and trigger a redraw. Of course the redraw would have to draw everything in the area of the view bring redrawn which is potentially slow.
What I would do if I had a series of lines I wanted to draw over a period of time is to have two subviews - they would cover the same area and the top one would have a transparent background. Have the top one draw just the line that I am currently animating and when it's finished, draw the full length of it in the lower view. Then repeat, animating the next line in the top view.
With the new API you can do that easily, using strokeEnd property of CGPath.
You can read more details here: What's the easiest way to animate a line?