Egret – 2D跑酷道具吸金币

在众多跑酷游戏中,除了跑酷元素以外还包括很多其他的道具效果,其中比较场景的包括:吸金币、加速、无敌等,这一章我们来做一下吸金币的效果。

  • 检索金币与豆小鸭的位置关系
  • 我们通过圆形扫描就可以,这里比较容易


    实现方式如下

    // 
    public AttractCoin() {
        var view:egret.DisplayObjectContainer = (this.app.viewManager.getView(ViewConst.Player));
        var dxy = view.getChildByName("DXY");
        var coins:Array> = new Array>();
        var width = 0;
        var height = 0;
        var num = this.coinContent.numChildren;
        var id_index = 0;
        for (var i = 0; i < num; i++) {
            var c_content:egret.DisplayObjectContainer = this.coinContent.getChildAt(i);
            for (var j = 0; j < c_content.numChildren; j++) {
                var coin = c_content.getChildAt(j);
                width = coin.width
                height = coin.height;
                coins.push([coin.x + c_content.x + this.coinContent.x, 
                    coin.y + c_content.y + this.coinContent.y]);
                /*App.DebugUtils.debugDraw(coin, "coin" + id_index, "0x00ff00", c_content.x + this.coinContent.x, 
                    c_content.y + this.coinContent.y, false);*/
                id_index = id_index + 1
            }
        }
        this._ctr.coinModel.checkCoinsInObject(coins, [dxy.x, dxy.y], 500, dxy.width, dxy.height);
    }
    // 
    public checkCoinsInObject(coins:Array>, player_position:Array, r:number, width?:number, height?:number) {
        // 遍历所有的Coins
        if (width == undefined) {
            width = 0;
        }
        if (height == undefined) {
            height = 0;
        }
        for (var key in coins) {
            var coin:Array = >coins[key];
            var lenQt = App.MathUtils.getDistanceQt(coin[0], coin[1], player_position[0] - width / 2, player_position[1] - height / 2);
            // App.DebugUtils.debugDrawPosition("test01", "0xff0000", coin[0] - width / 2, coin[1] - height / 2, width, height);
            if (lenQt <= r * r) {
                console.log("Attract");
            }
        }
    }
    

    这里的遍历方式都是可以进行优化的,比如碰撞检测时候我们也无需遍历所有的,可以进行优化。

  • 金币的移动
  • // 
    public checkCoinsInObject(player, coins:Array, coins_pos:Array>, player_position:Array, r:number, width?:number, height?:number) {
        // 遍历所有的Coins
        if (width == undefined) {
            width = 0;
        }
        if (height == undefined) {
            height = 0;
        }
        // 定义金币移动速度 (单位时间位移即可)
        var speed = [13, 13];
        // 设定最小距离
        var min_distance_sq = 10 * 10;
        var del_coins:Array = Array();
        for (var key in coins_pos) {
            var coin_pos:Array = >coins_pos[key];
            var coin = coins[key];
            var lenQt = App.MathUtils.getDistanceQt(coin_pos[0], coin_pos[1], player_position[0] - width / 2, player_position[1] - height / 2);
            if (lenQt <= r * r) {
                if (lenQt < min_distance_sq) {
                    // 吃掉金币
                    del_coins.push(coin);
                } else {
                    // 移动
                    if (coin_pos[0] < player.x) {
                        coin.x = coin.x + speed[0];
                    } else {
                        coin.x = coin.x - speed[0];
                    }
    
                    if (coin_pos[1] < player.y) {
                        coin.y = coin.y + speed[1];
                    } else {
                        coin.y = coin.y - speed[1];
                    }
                }
            }
        }
        for (var key in del_coins) {
            var del_coin = del_coins[key];
            del_coin.parent.removeChild(del_coin);
        }
    }
    

    大致效果如下:


    我们还是可以对吸金币进行一些效果的优化

    欢迎留言

    avatar
      Subscribe  
    Notify of