Simple example

From WeBWorK_wiki
Revision as of 16:05, 28 January 2009 by Dougensley (talk | contribs) (Before and after example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

ActionScript code for a simple graphing example:

On the stage should be an input textbox called txtFun for this version. Hitting the "enter" key after editing this box will update the screen. The ww version will get the expression to graph through a call to the config function, so we will be deleting this input textbox in the subsequent version.

import flashandmath.as3.tools.SimpleGraph;
var sxMin:String = "-5";
var sxMax:String = "5";
var syMin:String = "-5"; 
var syMax:String = "5";
var sFun:String =  txtFun.text;

var g:SimpleGraph = new SimpleGraph(480,360);
g.x = 20;
g.y = 40;
addChild(g);

var spPoint:Sprite = new Sprite();
spPoint.x = 5;
spPoint.y = 5;
spPoint.graphics.lineStyle(1,0);
spPoint.graphics.beginFill(0xAA0000);
spPoint.graphics.drawEllipse(-5,-5,10,10);
spPoint.graphics.endFill();
spPoint.addEventListener(MouseEvent.MOUSE_DOWN, pointClicked)
g.addChildToBoard(spPoint);

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
function keyPressed(kevt:KeyboardEvent):void {
   if (kevt.keyCode == Keyboard.ENTER) {
       sFun = txtFun.text;
       drawGraph();
   }
}

function drawGraph():void {
   g.setWindow(sxMin, sxMax, syMin, syMax);
   g.board.drawAxes();
   g.board.drawTicks();
   g.board.drawGrid();
   g.board.addLabels();
   g.graphRectangular(sFun,"x",1,2,0x0000CC);
}

drawGraph();

function pointClicked(mevt:MouseEvent):void {
   stage.addEventListener(MouseEvent.MOUSE_MOVE, pointMoved);
   stage.addEventListener(MouseEvent.MOUSE_UP, pointReleased);
}

function pointMoved(mevt:MouseEvent):void {
   spPoint.x = goodX(g.mouseX);
   spPoint.y = goodY(g.mouseY);
   mevt.updateAfterEvent();
}

function pointReleased(mevt:MouseEvent):void {
   stage.removeEventListener(MouseEvent.MOUSE_MOVE, pointMoved);
   stage.removeEventListener(MouseEvent.MOUSE_UP, pointReleased);
}

function goodX(nx:Number):Number {
   if (nx > 480) {
      return 480;
   }
   if (nx < 0) {
      return 0;
   }
   return nx;
}

function goodY(ny:Number):Number {
   if (ny > 360) {
      return 360;
   }
   if (ny < 0) {
      return 0;
   }
   return ny;
}