Question

I am a newbie in Quartz and I am fighting to understand this stuff apple say is very easy and straightforward.

I have created two CGLayers: one for a fixed background and another one for a sprite. I want this sprite to move.

Both the background context and the sprite context are drawn offscreen and I would like both to be seen on the screen.

To do that - and I am not sure if this is the correct way - I have did the following:

  1. I have created an UIImageView
  2. I have captured the layer's contents using

    resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    myView.image = resultingImage;

This shows me on the screen the contents of both quartz layers.

Now I have two problems:

  1. This approach is slow as hell
  2. When I move the layer, I have to repeat the mentioned code and EVEN DOING THIS, THE LAYER IS NOT MOVING!!!!

So, please, iPhone gurus out there, please tell me if there's another way to do this with quartz and what I have to do to see the sprite moving!!!!

thanks for any help!

Was it helpful?

Solution

The main reason to use CALayers is to get the GPU to composite directly onto the screen memory. Using UIGraphicsGetImageFromCurrentImageContext() results in the image being composited on the GPU, transferred to main memory and then back again to draw it on the screen which will be really slow. What you should be doing instead is making your new layers sublayers of your view's layer:

[self.view.layer addSublayer:myNewLayer];

For a good example of how to use CALayers in a game see: GeekGameBoard

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top