开发者

AS3 random point between two rectangles

开发者 https://www.devze.com 2023-02-05 11:02 出处:网络
I\'m making a top view zombie survival game in AS3. I want to spawn the enem开发者_C百科ies in waves defined by \"frames\", or the difference between a bigger rectangle and a smaller one within it, li

I'm making a top view zombie survival game in AS3. I want to spawn the enem开发者_C百科ies in waves defined by "frames", or the difference between a bigger rectangle and a smaller one within it, like a picture frame. Right now here is my code, which is pretty inefficient. I was wondering if there was a better way. This code doesn't work right either, all the enemies spawn in the bottom right of the screen:

    public static function waveOne():Point {

        var inner:Rectangle = Waves.WAVE_ONE_RECTANGLE_1;
        var outer:Rectangle = Waves.WAVE_ONE_RECTANGLE_2;

        var x:Number = Math.random();
        var y:Number = Math.random();

        x = x * outer.width;
        y = y * outer.height;

        trace(x + " " + y);

        if (((x > outer.left && x < inner.left) || (x > inner.right && x < outer.right)) && ((y > outer.top && y < inner.top) || (y > inner.bottom && y < outer.bottom))) {
            return(new Point(x, y));
        }
        else {
            return waveOne();
        }
    }


A simple solution would be to define 4 rectangles using the inner rectangle and the outer rectangle. (left,right,top,bottom) First, you select randomly a rectangle and then you juste have to find your point inside that rectangle.

0

精彩评论

暂无评论...
验证码 换一张
取 消