ripsraps
ripsraps

Reputation: 19

Increasing size on a graphic circle

I'm currently trying to make a circle using graphics.linestyle and graphics.drawCircle with a mouseclick. After the mouseclick I want the circle to expand on stage until the radius reach 150. I've tried with this code, but not getting it to work.

import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.events.Event;

var circle:Sprite = new Sprite();

knapp.addEventListener(MouseEvent.CLICK, push);

function push (evt:MouseEvent) {

    var radius:Number = 10;
    var incRadius:Number = 3;

    circle.graphics.lineStyle(3, 0xff000, 1);
    circle.graphics.drawCircle(200, 200, radius);

    addChild(circle);

    circle.addEventListener(Event.ENTER_FRAME, increase);
    function increase (evt:Event) {
        if (radius <= 150) {
            radius += incRadius;
        }
    }
}

Upvotes: 0

Views: 42

Answers (1)

Atriace
Atriace

Reputation: 2558

  • Pop out the nested function into the global namespace.
  • Make recurring variables global.
  • After you increase the radius, you need to redraw the circle.
  • Because you are only drawing the edge, you need to clear the circle on every update
  • Removed unused event listeners when your animation is complete.

This will fix it. You can run it in a blank project and it will compile and work. Just click anywhere on the background, and it'll run.

import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.events.Event;

var circle:Sprite = new Sprite();
addChild(circle);
var radius:Number = 10;
var incRadius:Number = 3;

stage.addEventListener(MouseEvent.CLICK, push);

function push(e:MouseEvent) {
    radius = 10;

    circle.graphics.clear();
    circle.graphics.lineStyle(3, 0xff000, 1);
    circle.graphics.drawCircle(0, 0, radius);
    circle.x = stage.mouseX;
    circle.y = stage.mouseY;

    circle.addEventListener(Event.ENTER_FRAME, increase);
}

function increase(e:Event) {
    radius += incRadius;

    if (radius > 150) {
        circle.removeEventListener(Event.ENTER_FRAME, increase);
    } else {
        circle.graphics.clear();
        circle.graphics.lineStyle(3, 0xff000, 1);
        circle.graphics.drawCircle(0, 0, radius);
    }
}

Upvotes: 1

Related Questions