• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Access of possibly undefined property graphics through a reference with static type flash.display:Stage.

New Here ,
Sep 21, 2015 Sep 21, 2015

Copy link to clipboard

Copied

Having trouble doing a coding exercise. I'm trying to follow the commented-out instructions by putting my answers right below them, but I keep getting a compiler error saying "Scene 1, Layer 'actions', Frame 1, Line 42, Column 13 1119: Access of possibly undefined property graphics through a reference with static type flash.display:Stage." I have no idea what I'm supposed to put in front of the .graphics on with(stage.graphics) to make the code work, or whatever else I'm supposed to do. Can anyone help me?

import flash.display.Graphics;

var xDiff:Number;
var yDiff:Number;
var distance:Number;

stage.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
stage.addEventListener(MouseEvent.MOUSE_UP, endDrag);
stage.addEventListener(MouseEvent.MOUSE_MOVE, enterFrm);

function beginDrag(evt:MouseEvent):void
{
if(evt.target.name == "hat" || evt.target.name == "ball")
    {
   evt.target.startDrag();
    }
}

function endDrag(event:MouseEvent):void
{
stopDrag();
}

function calcDistance(){
//Using the Pythagorean theorem, calculate the distance between the ball and the hat
//calculate and store the distance between the ball and the hat in the distance variable
//use the xDiff and yDiff variables in your calculation
xDiff = hat.x - ball.x;
yDiff = hat.y - ball.y;

distance = Math.sqrt(Math.pow(xDiff, 2) + Math.pow(yDiff, 2));

//place the distance in the distanceAmount text field, don't forget to use the String method and round off the calculated distance
distanceAmount.text = String(distance);
//If you prefer you may use the Point.distance method instead of the Pythagorean theorem
}

function enterFrm(event:MouseEvent):void
{
with(stage.graphics)
{
//use the graphics class to clear the screen
clear();

//use the graphics class and the lineStyle method to set a line style (use 3 for the thickness of the line, 0xFF0000 for the color and 1 for the alpha value
lineStyle(3, 0xFF0000, 1);

//use the graphics class and the moveTo method to move to the hat
moveTo(hat.x, 0);

//use the graphics class and the lineTo method to draw a line to the ball
lineTo(ball.x, ball.y);
}

calcDistance();
}

TOPICS
ActionScript

Views

637

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2015 Sep 22, 2015

Copy link to clipboard

Copied

the error message is clear.  you can't use this:

with(stage.graphics)

because the stage doesn't have a graphics property.  only shape and sprite objects (and those that inherit from them like movieclip) can use the graphics property.

instead of stage, create a sprite and add it to the stage and apply your draw methods to the sprite.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 25, 2015 Sep 25, 2015

Copy link to clipboard

Copied

LATEST

Just a mention re optimization - since you're calling calcDistance every frame, and seemingly calling it only from within your frame loop, I would inline the distance calc code that frame loop code. I'd also remove the call to Math.sqrt() and use the distance squared instead.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines