Cocos制作动态生成内容的列表

一、创建空节点,或者直接创建ScrollView

如果不想显示滚动条的可以将scrollBar组件隐藏掉,尽量不要删除,只做隐藏处理即可。

二、调整Scroll View组件大小

这里可以根据自己需求调整大小

同时修改view的大小

三、修改item样式并生成预制体

我这里直接创建了一个Layout组件,里边放了两个Label

Layout组件大小设置一下,增加Sprite组件为Layout增加背景

将Layout改名为TestItem,然后拖到prefab文件夹下生成item预制体,然后删除content下的item和TestItem

四、创建列表脚本

创建TestList脚本

在脚本中创建cntent节点,testItem预制体和testList的ScrollView

@ccclass('TestList')
export class TestList extends Component {

    //ScrollView下的cntent
    @property(Node)
    cntent:Node = null!;

    //制作的列表条目预制体
    @property(Prefab)
    testItem:Prefab = null!;

    //ScrollView
    @property(ScrollView)
    testList:ScrollView = null!;

    start() {
        
    }

    update(deltaTime: number) {
        
    }
}

回到cocos场景编辑器,将脚本挂载到Scroll View组件上,再将对应的组件拖到对应的位置

五、在content组件上增加Layout组件

六、编写脚本,做虚拟数据展示

import { _decorator, Component, instantiate, Label, Node, Prefab, ScrollView } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('TestList')
export class TestList extends Component {

    @property(Node)
    cntent:Node = null!;

    @property(Prefab)
    testItem:Prefab = null!;

    @property(ScrollView)
    testList:ScrollView = null!;

    start() {
        this.initData();
    }

    update(deltaTime: number) {
        
    }

    initData(){
        console.log('设置数据');
        for (let index = 0; index < 100; index++) {
           const title = "标题"+index;
           const time = "2023-12-"+index;
           this.showList(title,time);
        }
    }

    showList(title:string,time:string){
        const item = instantiate(this.testItem);
        item.getChildByName('title').getComponent(Label).string = title;
        item.getChildByName('time').getComponent(Label).string = time;

        this.cntent.addChild(item);
    }
}

七、运行测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cocos2d-x 是一个开源的跨平台游戏开发框架,支持 C++、Lua 和 JavaScript 等语言。以下是一个简单的飞机大战游戏的制作流程: 1. 创建一个新的 Cocos2d-x 项目,在命令行中使用以下命令: ``` cocos new MyGame -p com.your_company.mygame -l cpp -d /path/to/your/project ``` 其中,`MyGame` 是项目的名称,`com.your_company.mygame` 是项目的包名,`/path/to/your/project` 是项目的路径。 2. 在 `Classes` 文件夹下创建游戏场景类和游戏层类。游戏场景类负责管理游戏层,游戏层类负责绘制游戏界面和处理用户输入事件。可以参考以下代码: ```c++ class GameScene : public cocos2d::Scene { public: static cocos2d::Scene* createScene(); virtual bool init(); CREATE_FUNC(GameScene); }; class GameLayer : public cocos2d::Layer { public: virtual bool init(); void update(float delta); CREATE_FUNC(GameLayer); }; ``` 3. 在游戏层中添加背景图和玩家飞机。可以使用 `Sprite` 类来加载图片资源,并使用 `addChild()` 方法将其添加到层中。例如: ```c++ auto background = Sprite::create("background.png"); background->setPosition(visibleSize.width / 2, visibleSize.height / 2); addChild(background); player = Sprite::create("player.png"); player->setPosition(visibleSize.width / 2, player->getContentSize().height / 2); addChild(player); ``` 4. 实现玩家飞机的移动和射击功能。可以使用 `EventKeyboard` 类来监听键盘事件,并在 `update()` 方法中更新玩家飞机的位置和子弹的位置。例如: ```c++ void GameLayer::update(float delta) { // 移动玩家飞机 if (isKeyPressed(EventKeyboard::KeyCode::KEY_UP_ARROW)) { player->setPositionY(player->getPositionY() + 5.0f); } if (isKeyPressed(EventKeyboard::KeyCode::KEY_DOWN_ARROW)) { player->setPositionY(player->getPositionY() - 5.0f); } if (isKeyPressed(EventKeyboard::KeyCode::KEY_LEFT_ARROW)) { player->setPositionX(player->getPositionX() - 5.0f); } if (isKeyPressed(EventKeyboard::KeyCode::KEY_RIGHT_ARROW)) { player->setPositionX(player->getPositionX() + 5.0f); } // 发射子弹 if (isKeyPressed(EventKeyboard::KeyCode::KEY_SPACE)) { auto bullet = Sprite::create("bullet.png"); bullet->setPosition(player->getPositionX(), player->getPositionY() + player->getContentSize().height / 2); addChild(bullet); auto moveBy = MoveBy::create(1.0f, Vec2(0, visibleSize.height)); auto removeSelf = RemoveSelf::create(); auto sequence = Sequence::create(moveBy, removeSelf, nullptr); bullet->runAction(sequence); } } ``` 5. 添加敌机和碰撞检测功能。可以使用定时器来定期生成敌机,并使用 `Rect` 类来判断玩家飞机和子弹是否与敌机发生碰撞。例如: ```c++ void GameLayer::addEnemy(float delta) { auto enemy = Sprite::create("enemy.png"); float x = CCRANDOM_0_1() * visibleSize.width; enemy->setPosition(x, visibleSize.height + enemy->getContentSize().height / 2); addChild(enemy); auto moveBy = MoveBy::create(2.0f, Vec2(0, -visibleSize.height - enemy->getContentSize().height)); auto removeSelf = RemoveSelf::create(); auto sequence = Sequence::create(moveBy, removeSelf, nullptr); enemy->runAction(sequence); } void GameLayer::checkCollision() { for (auto enemy : enemies) { if (player->getBoundingBox().intersectsRect(enemy->getBoundingBox())) { // 碰撞处理 } for (auto bullet : bullets) { if (bullet->getBoundingBox().intersectsRect(enemy->getBoundingBox())) { // 碰撞处理 } } } } ``` 6. 最后,在游戏场景类的 `init()` 方法中添加游戏层、定时器和碰撞检测函数。例如: ```c++ bool GameScene::init() { if (!Scene::init()) { return false; } auto gameLayer = GameLayer::create(); addChild(gameLayer); schedule(schedule_selector(GameLayer::addEnemy), 1.0f); scheduleUpdate(); return true; } void GameLayer::update(float delta) { checkCollision(); } ``` 以上是一个简单的飞机大战游戏的制作流程,具体的实现细节还需要结合实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值