cocos2d node绘制两个
时间: 2023-10-03 07:01:50 浏览: 153
如果您想在Cocos2d中绘制两个节点,您可以按照以下步骤操作:
1. 创建两个节点对象
```cpp
auto node1 = Node::create();
auto node2 = Node::create();
```
2. 分别将这两个节点添加到场景中
```cpp
this->addChild(node1);
this->addChild(node2);
```
3. 对每个节点设置位置、颜色、大小等属性
```cpp
node1->setPosition(Vec2(100, 100));
node1->setColor(Color3B::RED);
node1->setScale(2.0f);
node2->setPosition(Vec2(200, 200));
node2->setColor(Color3B::BLUE);
node2->setScale(1.5f);
```
4. 在每个节点中添加需要绘制的内容,例如精灵、标签等
```cpp
auto sprite1 = Sprite::create("sprite1.png");
node1->addChild(sprite1);
auto label1 = Label::createWithTTF("Node 1", "fonts/arial.ttf", 24);
node1->addChild(label1);
auto sprite2 = Sprite::create("sprite2.png");
node2->addChild(sprite2);
auto label2 = Label::createWithTTF("Node 2", "fonts/arial.ttf", 24);
node2->addChild(label2);
```
5. 运行程序,即可看到两个节点的内容被绘制在屏幕上
```cpp
bool MyScene::init()
{
if (!Scene::init())
{
return false;
}
auto node1 = Node::create();
auto node2 = Node::create();
this->addChild(node1);
this->addChild(node2);
node1->setPosition(Vec2(100, 100));
node1->setColor(Color3B::RED);
node1->setScale(2.0f);
node2->setPosition(Vec2(200, 200));
node2->setColor(Color3B::BLUE);
node2->setScale(1.5f);
auto sprite1 = Sprite::create("sprite1.png");
node1->addChild(sprite1);
auto label1 = Label::createWithTTF("Node 1", "fonts/arial.ttf", 24);
node1->addChild(label1);
auto sprite2 = Sprite::create("sprite2.png");
node2->addChild(sprite2);
auto label2 = Label::createWithTTF("Node 2", "fonts/arial.ttf", 24);
node2->addChild(label2);
return true;
}
void MyScene::onEnter()
{
Scene::onEnter();
}
```
阅读全文
相关推荐


















