AS2 Рисование кривых для холмов - работает случайным образом - PullRequest
0 голосов
/ 30 июня 2011

У меня есть массив, который содержит точки для рисования нескольких холмов.Когда я просто рисую прямые линии, это работает нормально, но это неестественно, поэтому я хочу сделать верхнюю / нижнюю части холма изогнутыми.

for(i = 0; i < rPoints.length - 1; i++){
    gamebg.lineStyle(1,0x000000,100);
    gamebg.moveTo(rPoints[i][0] + 45, rPoints[i][1]); //Doesn't directly move to a point so there is empty space for the curved parts.
    if(rPoints[i+1][1] > rPoints[i][1]){ //Determines if it is the top part of a hill or a bottom part, compares y
        gamebg.lineTo(rPoints[i+1][0], rPoints[i+1][1]);
        gamebg.moveTo(rPoints[i+1][0], rPoints[i+1][1]);
        //I didn't add a curveTo here because I only wanted to test it on one so I can make changes easier
    } else {
        gamebg.lineTo(rPoints[i+1][0], rPoints[i+1][1]);
        gamebg.moveTo(rPoints[i+1][0], rPoints[i+1][1]);
        gamebg.curveTo(rPoints[i+1][0]+22, rPoints[i+1][1]-25, rPoints[i+1][1]+45, rPoints[i+1][1]);
    }
}

Когда я запускаю код, кажется, что он работает несколько раз,

Когда он работает, он работает только для первого.http://i.stack.imgur.com/5AeG4.png

Спасибо!

1 Ответ

0 голосов
/ 01 июля 2011

гораздо проще определить части холма по порядку Массива, содержащего точки. В этом случае вы должны выполнить итерации 3 вместо 1. Я протестировал следующий код, он работал нормально. * Николай

/* 
 * ActionScript 2
 * 
 * Drawing some Hills :-) 
 * 
 * The following Loop draws Hills using an Array containing
 * a multiple of 3 Points 
 *  
 *            p5
 *    p2      /\
 *    /\     /  \
 *   /  \   /    \
 *  p1  p3 p4    p6
 * 
 */

var rPoints = [
                // First Hill
                [50,200],
                [100,10],
                [150,200],
                // Second Hill
                [100,200],
                [150,80],
                [200,200],
                // Will not be drawn
                [500,200]
        ];


for(i = 0; i <= rPoints.length-3; i+=3){    

    gamebg.lineStyle(1,0x000000,100);

    // Move to the 1st Point
    gamebg.moveTo(rPoints[i][0], rPoints[i][1]);                
    // Curve to 2nd Point (For a smooth curve the Control Point should be in the Top Left Corner)
    gamebg.curveTo(rPoints[i][0], rPoints[i+1][1], rPoints[i+1][0], rPoints[i+1][1]);
    // Curve to 3rd Point (For a smooth curve the Control Point should be in the Top Right Corner)
    gamebg.curveTo(rPoints[i+2][0], rPoints[i+1][1], rPoints[i+2][0], rPoints[i+2][1]);
    // Close the shape (If you want to)
    gamebg.lineTo(rPoints[i][0], rPoints[i][1]);    
}
...