IOS-Cocos2d game development 7] In cocos2d Add/delete system components, and solve the view setting transparent will affect the view of other components! [Fixed the bug of adding cocos2d animation components to stop playing on April 9, November 28]

Source: Internet
Author: User

Li huaming himiOriginal, reprinted must be explicitly noted:
Reprinted from[Heimi gamedev block]Link:

November 28 :---------------------------------

Some children's shoes say that cocos2d animation does not understand cocos2d after adding the trim component (uiscrollview). I tested it and it is true that the following is a solution:

Search for ccdirectorios, find the ccdirectorios. M class, and find the following code:

////If you want to attach the opengl view into UIScrollView//  uncomment this line to prevent 'freezing'.//It doesn't work on with the Fast Director////[[NSRunLoop currentRunLoop] addTimer:animationTimer// forMode:NSRunLoopCommonModes];

Then, comment out the following line of code in the appdelegate. M class!

//if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )



It seems that some time has not been updated, and the main projects are in a hurry, but now cocos2d is basically completed, and the progress is very smooth. Here we will introduce some problems encountered by himi;

This chapter introduces two knowledge points:

1. Add system components in cocos2d. (In this example, add uiview and nest some views)

2. After the view settings are transparent, other components in the view are also transparent. The detailed problem is as follows)

"How to set the background image to translucent" the current interface is probably like this: the bottom of the uiview, which has its own background image, and then there is a second view on this uiview, this view also has a background image. The effect to be achieved is that the image of the second view will not overwrite the background image of the bottom view. I don't think I can change the Alpha value of the previous view, because the previous view also has its own components. If so, all the elements in the above view will be transparent, but this is not what I want... Transparent is only the second layer of the background image.

The above are the questions of netizens. In fact, the problem is the second question we will discuss today;

OK. First, we will introduce how to add system components in cocos2d;

First, create a cocos2d project. The cocos2d version I use here is 1.0.0.

Create a uiviewcontroller class named myuiview (this file creation step is not mentioned ~)

Shows the entire project directory:

Add something in myuiview. XIB, otherwise it cannot be clearly displayed. OK, double-click the XIB file to write it (My xcode version is 4.1 for Lion)

Here I added a view and placed a button (to facilitate the second question in this chapter), as shown in:

OK, save, and then write the code that shows the system components in cocos2d: helloworldlayout. m is as follows:

MyUIView *view = [[MyUIView alloc] initWithNibName:@"MyUIView" bundle:nil];[[[CCDirector sharedDirector] openGLView] addSubview:view.view];

These two statements are written in the constructor in the default scenario class provided by cocos2d:

-(id) init{if( (self=[super init])) {                MyUIView *view = [[MyUIView alloc] initWithNibName:@"MyUIView" bundle:nil];[[[CCDirector sharedDirector] openGLView] addSubview:view.view];        }return self;}

Run the following command to check the effect:

It's easy to delete the added components! The following code:

[self.updateUIView.view removeFromSuperview];

In this case, you need to add [self. updateuiview. View release]; to release the instance from the memory ~

Next we will explain the second point of knowledge in this blog;

To help you better understand the problem, I set a background image for the button and set a gray background for the view containing the button, such:

OK. Now we want to set the view to be translucent, OK, set Alpha to 0. 5. Then run the command to see the following:

This effect is obviously incorrect, because we want a translucent view, and the buttons do not need to be transparent !!!!!

When I encountered this problem, I started various attempts, because my project had a lot of views. I tried to add an imageview to the view and set the image to be as transparent as the background image, however, since there were many views, they finally failed. Then they tried various duniang, Google, and so on. They also went to the corresponding group and asked if they could find a solution, but finally let me try out the solution;

In fact, the alpha of view is a transparency for the entire view (including all components). If you only modify the transparency of view, you only need to change its opacity attribute!

Click your view, click its background attribute, and then click other, such:

Change the opacity attribute below it to 50%, OK, save the current XIB file, and run the project! As follows:

This is the expected result. The view is 50% transparent ~ Components are not affected;

In fact, this blog post is simple and detailed, but the Details determine the success or failure. I hope you will encounter similar details and try your best not to accumulate questions for yourself, otherwise, it will bring great obstacles to future improvement;

OK. Here we are, kids shoes, scatter a bit of oil ~

Source code:

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.