1

Здравствуйте! Хочу, чтобы снег падал, пока падает одна снежинка.

public Scene onLoadScene() {
// Таймер
myTimer = new Timer();myTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            TimerMethod();
        }

    }, 0, 1000);

// Снежинка
   Random randomGenerator = new Random();
   Random randomGeneratorXshift = new Random();
   pX = randomGenerator.nextInt(CAMERA_WIDTH);
// Снежинка создается в координате (Х) любое значение в пределах ширины камеры
   shiftX = randomGeneratorXshift.nextInt(100) - 50;
   AnimatedSprite snow = new AnimatedSprite (pX, 1, this.mTextureSnowRegion);
   final PhysicsHandler physicsHandler = new PhysicsHandler(snow);
   snow.registerUpdateHandler(physicsHandler);
   physicsHandler.setVelocity(shiftX, DEMO_VELOCITY);
   scene.getLastChild().attachChild(snow);
return scene;
}

private void TimerMethod()
{
    //This method is called directly by the timer
    //and runs in the same thread as the timer.

    //We call the method that will work with the UI
    //through the runOnUiThread method.
    this.runOnUiThread(Timer_Tick);
}

private Runnable Timer_Tick = new Runnable() {
    public void run() {

    //This method runs in the same thread as the UI.

    //Do something to the UI thread here

    }
};

Куда и как я должен вставить код генерирования снежинки, чтобы таймер заработал?

1 ответ 1

2

Так вы же создаёте только одну снежинку. Код

shiftX = randomGeneratorXshift.nextInt(100) - 50;    
AnimatedSprite snow = new AnimatedSprite (pX, 1, this.mTextureSnowRegion);    
final PhysicsHandler physicsHandler = new PhysicsHandler(snow);    
snow.registerUpdateHandler(physicsHandler);    
physicsHandler.setVelocity(shiftX, DEMO_VELOCITY);    
scene.getLastChild().attachChild(snow);

нужно вызвать несколько раз в цикле.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.