Skip to main content
Participating Frequently
June 18, 2015
Answered

Need help integration a AS3 drawing game into a .fla

  • June 18, 2015
  • 1 reply
  • 4159 views

Hi,

I created a drawing game by following a tutorial on this website : http://code.tutsplus.com/tutorials/create-a-basic-drawing-application-in-flash--active-1627

It was pretty straight forward. (you can download the source file to have the same code as me and save time).

Now to the fun part!

I previously created an iPad app in Flash. Works great by the way and is published on the App Store. (Search for : Henri Godon -> free app)

My client wants this drawing game to be incorporated in his app with a possibility of 3 different images to color opon. Which is pretty easy to do.

I thought that just doing a simple copy/paste and including the scripts would do the trick.... WRONG! I keep getting errors once I try to export it or test it out.

My App works great without the game. And the game works great alone. It's only one I try to embed it that I'm stuck.

Here are my errors, hope someone has an easy DIY way that I'm not aware of since I have never done this before.

ERROR 1:

1037 : Packages cannot be nested.

here is what it is referring to in the Main.as script :

package

{

  import PNGEncoder;

  import flash.display.MovieClip;

  import flash.display.Shape;

  import flash.display.DisplayObject;

  import flash.text.TextField;

  import flash.text.TextFormat;

  import flash.text.TextFieldType;

  import flash.text.TextFieldAutoSize;

  import flash.display.BitmapData;

  import flash.geom.ColorTransform;

  import flash.events.MouseEvent;

  import flash.events.Event;

  import flash.utils.ByteArray;

  import flash.net.FileReference;

  public class Main extends MovieClip

  {

ERROR 2:

Even if I try to remove that part, then I get an other error saying

1114: The public attribute can only be used inside a package. (which is kinda obvious)

1084: Syntax error: expecting leftbrace before var.

So nothing works after that...

I added the Main.as in the ActionScript setting in the Document class. So I know it is loaded in a way, but after that I'm stuck!

Please help out! I'll do anything.

This topic has been closed for replies.
Correct answer kglad

‌hi kglad,

yes yes I can create a swf with it. How would I import it so it could play on iPad?

not sure what a loader class is or how to add the class to my project.

feeling like a noob here. I just transferred from using Director lingo to learning flash in a week.

DIrector can't make 64bit iOS apps... Learning curve is kinda steep.


var loader:Loader=new Loader();

var lc:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain,null);

loader.load(new URLRequest('yourgameswf.swf'),lc);

addChild(loader);

1 reply

kglad
Community Expert
June 18, 2015

you have mismatched curly brackets in your class.

Participating Frequently
June 18, 2015

not too sure where... Here is the complet Main.as code :

package

{

  import PNGEncoder;

  import flash.display.MovieClip;

  import flash.display.Shape;

  import flash.display.DisplayObject;

  import flash.text.TextField;

  import flash.text.TextFormat;

  import flash.text.TextFieldType;

  import flash.text.TextFieldAutoSize;

  import flash.display.BitmapData;

  import flash.geom.ColorTransform;

  import flash.events.MouseEvent;

  import flash.events.Event;

  import flash.utils.ByteArray;

  import flash.net.FileReference;

  public class Main extends MovieClip

  {

  /* Variables */

  /* Pencil Tool shape, everything drawed with this tool and eraser is stored inside board.pencilDraw */

  var pencilDraw:Shape = new Shape();

  /* Text format */

  var textformat:TextFormat = new TextFormat();

  /* Colors */

  var colorsBmd:BitmapData;

  var pixelValue:uint;

  var activeColor:uint = 0x000000;

  /* Active var, to check wich tool is active */

  var active:String;

  /* Shape size color */

  var ct:ColorTransform = new ColorTransform();

  public function Main():void

  {

  textformat.font = "Quicksand Bold Regular";

  textformat.bold = true;

  textformat.size = 16;

  convertToBMD();

  addListeners();

  /* Hide tools highlights */

  pencil.visible = false;

  hideTools(eraser, txt);

  }

  /* Pencil Tool */

  private function PencilTool(e:MouseEvent):void

  {

  /* Quit active tool */

  quitActiveTool();

  /* Set to Active */

  active = "Pencil";

  /* Listeners */

  board.addEventListener(MouseEvent.MOUSE_DOWN, startPencilTool);

  board.addEventListener(MouseEvent.MOUSE_UP, stopPencilTool);

  /* Highlight */

  highlightTool(pencil);

  hideTools(eraser, txt);

  ct.color = activeColor;

  shapeSize.transform.colorTransform = ct;

  }

  private function startPencilTool(e:MouseEvent):void

  {

  pencilDraw = new Shape();

  board.addChild(pencilDraw);

  pencilDraw.graphics.moveTo(mouseX, mouseY);

  pencilDraw.graphics.lineStyle(shapeSize.width, activeColor);

  board.addEventListener(MouseEvent.MOUSE_MOVE, drawPencilTool);

  }

  private function drawPencilTool(e:MouseEvent):void

  {

  pencilDraw.graphics.lineTo(mouseX, mouseY);

  }

  private function stopPencilTool(e:MouseEvent):void

  {

  board.removeEventListener(MouseEvent.MOUSE_MOVE, drawPencilTool);

  }

  /* Eraser Tool */

  private function EraserTool(e:MouseEvent):void

  {

  /* Quit active tool */

  quitActiveTool();

  /* Set to Active */

  active = "Eraser";

  /* Listeners */

  board.addEventListener(MouseEvent.MOUSE_DOWN, startEraserTool);

  board.addEventListener(MouseEvent.MOUSE_UP, stopEraserTool);

  /* Highlight */

  highlightTool(eraser);

  hideTools(pencil, txt);

  ct.color = 0x000000;

  shapeSize.transform.colorTransform = ct;

  }

  private function startEraserTool(e:MouseEvent):void

  {

  pencilDraw = new Shape();

  board.addChild(pencilDraw);

  pencilDraw.graphics.moveTo(mouseX, mouseY);

  pencilDraw.graphics.lineStyle(shapeSize.width, 0xFFFFFF);

  board.addEventListener(MouseEvent.MOUSE_MOVE, drawEraserTool);

  }

  private function drawEraserTool(e:MouseEvent):void

  {

  pencilDraw.graphics.lineTo(mouseX, mouseY);

  }

  function stopEraserTool(e:MouseEvent):void

  {

  board.removeEventListener(MouseEvent.MOUSE_MOVE, drawEraserTool);

  }

  /* Text Tool */

  private function TextTool(e:MouseEvent):void

  {

  /* Quit active tool */

  quitActiveTool();

  /* Set to Active */

  active = "Text";

  /* Listener */

  board.addEventListener(MouseEvent.MOUSE_UP, writeText);

  /* Highlight */

  highlightTool(txt);

  hideTools(pencil, eraser);

  }

  private function writeText(e:MouseEvent):void

  {

  var textfield = new TextField();

  textfield.type = TextFieldType.INPUT;

  textfield.autoSize = TextFieldAutoSize.LEFT;

  textfield.selectable = false;

  textfield.defaultTextFormat = textformat;

  textfield.textColor = activeColor;

  textfield.x = mouseX;

  textfield.y = mouseY;

  stage.focus = textfield;

  board.addChild(textfield);

  }

  /* Clear Tool */

  private function clearBoard(e:MouseEvent):void

  {

  /* Create a blank rectangle on top of everything but board */

  var blank:Shape = new Shape();

  blank.graphics.beginFill(0xFFFFFF);

  blank.graphics.drawRect(0, 0, board.width, board.height);

  blank.graphics.endFill();

  board.addChild(blank);

  }

  /* Default colors function */

  private function convertToBMD():void

  {

  colorsBmd = new BitmapData(colors.width,colors.height);

  colorsBmd.draw(colors);

  }

  private function chooseColor(e:MouseEvent):void

  {

  pixelValue = colorsBmd.getPixel(colors.mouseX,colors.mouseY);

  activeColor = pixelValue;//uint can be RGB!

  ct.color = activeColor;

  shapeSize.transform.colorTransform = ct;

  }

  /* Quit active function */

  private function quitActiveTool():void

  {

  switch (active)

  {

  case "Pencil" :

  board.removeEventListener(MouseEvent.MOUSE_DOWN, startPencilTool);

  board.removeEventListener(MouseEvent.MOUSE_UP, stopPencilTool);

  case "Eraser" :

  board.removeEventListener(MouseEvent.MOUSE_DOWN, startEraserTool);

  board.removeEventListener(MouseEvent.MOUSE_UP, stopEraserTool);

  case "Text" :

  board.removeEventListener(MouseEvent.MOUSE_UP, writeText);

  default :

  }

  }

  /* Highlight active Tool */

  private function highlightTool(tool:DisplayObject):void

  {

  tool.visible=true;

  }

  private function hideTools(tool1:DisplayObject, tool2:DisplayObject):void

  {

  tool1.visible=false;

  tool2.visible=false;

  }

  /* Change shape size */

  private function changeShapeSize(e:MouseEvent):void

  {

  if (shapeSize.width >= 50)

  {

  shapeSize.width = 1;

  shapeSize.height = 1;

  /* TextFormat */

  textformat.size = 16;

  }

  else

  {

  shapeSize.width += 5;

  shapeSize.height=shapeSize.width;

  /* TextFormat */

  textformat.size+=5;

  }

  }

  private function addListeners():void

  {

  pencilTool.addEventListener(MouseEvent.MOUSE_UP, PencilTool);

  eraserTool.addEventListener(MouseEvent.MOUSE_UP, EraserTool);

  textTool.addEventListener(MouseEvent.MOUSE_UP, TextTool);

  clearTool.addEventListener(MouseEvent.MOUSE_UP, clearBoard);

  colors.addEventListener(MouseEvent.MOUSE_UP, chooseColor);

  sizePanel.addEventListener(MouseEvent.MOUSE_UP, changeShapeSize);

  shapeSize.addEventListener(MouseEvent.MOUSE_UP, changeShapeSize);

  }

  }

}

Participating Frequently
June 24, 2015

not really.

are you publishing those swfs that load without error?

can you publish your main (loading) swf without error?


I can publish the Main .FLA without error but, I can't publish the Dessin-Henri, Dessin-Cowboy, and Dessin-Poster without error.

This is what I get when publish it in strict mode and warning mod on :

Scene 1, Layer 'Actions', Frame 1, Line 5, Column 11180: Call to a possibly undefined method Loader.

Scene 1, Layer 'Actions', Frame 1, Line 1, Column 11120: Access of undefined property dh_btn.

This is the script I have in the Actions layer frame 1

dh_btn.addEventListner(MouseEvent.CLICK, backF);

function backF(e: MouseEvent): void {

  Loader(this.parent).unloadAndStop();

}

I even triple checked in all of the 3 Dessin-xxx.swf to make sure there wasn't a duplicate of the btn.

I know we're on the verge of getting it!