How do I debug actionscript in ming?

David Marceau <[email protected]> Mon, 29 Dec 2003 03:30:41 -0500
Newsgroups gmane.comp.web.ming.general
Message-ID <[email protected]>
This c code is inspired from the perl example in Shawn Wallace's perl graphics
programming book.
Feel free to give constructive criticisms on all bad form.  
I know there's lots by the way since this was a quick try.

It's the astral tresspassers sample but it isn't quite perfect.  
I am trying to fix:
-Game Over text to get displayed
-mouse moving the gun
-mouse clicking launches a bullet

In other words I have a feeling the _setName isn't successful or getting set for
the wrong level/kind of object.

The only real problem I have is I would like to debug the actionscript with ming
on a linux box but it's not evident.

The ming astral.swf gets generated with success.  The debugging I would like to
do is from within the web browser.  Is there such beast as debugoutput commands
done from within actionscript that get redirected to a browser java console
somehow?

Thanks for any tips.

Cheers,
David Marceau



#include "ming.h"
SWFShape makeRect(SWFShape s, int width, int height, int dx, int dy, int r, int
g, int b, int opaqueness);

typedef struct gunDisplayItemType
{
  SWFDisplayItem i;
  int x;
  int y;
  double rot;
  double scale;
} gunDisplayItem;

typedef struct AlienDisplayItemType
{
  SWFDisplayItem i;
  int x;
  int y;
  double rot;
  double scale;
} AlienDisplayItem;

typedef struct BulletDisplayItemType
{
  SWFDisplayItem i;
  int x;
  int y;
  double rot;
  double scale;
} BulletDisplayItem;

typedef struct ScreenButtonItemType
{
  SWFDisplayItem i;
  int x;
  int y;
  double rot;
  double scale;
} ScreenButtonItem;

SWFShape makeRect(SWFShape s, int width, int height, int dx, int dy, int r, int
g, int b, int opaqueness)
{
  SWFShape_setRightFill(s, SWFShape_addSolidFill(s, r, g, b, 0xff));
  SWFShape_movePenTo(s, dx, dy);
  SWFShape_drawLine(s, width, 0);
  SWFShape_drawLine(s, 0, height);
  SWFShape_drawLine(s, -width, 0);
  SWFShape_drawLine(s, 0, -height);
  return s;
}

int main()
{
  SWFMovie myMovie;

  /* ok initialize the ming library */
  Ming_init();

  /* Flash 5.0 format player please (uncompressed) */
  Ming_useSWFVersion(5);

  /* ok set the scale for ming */
  Ming_setScale(20.0);

  /* ok create the player's gun */
  /* two rectangles */
  SWFShape sPlayerGun;
  sPlayerGun = newSWFShape();
  makeRect(sPlayerGun, 10, 10, -5, 0, 255, 0, 0, 255);
  makeRect(sPlayerGun, 40, 10, -20, 10, 255, 0, 0, 255);

  /* now create the blue alien with two different frames for basic 1-2 leg
movement */
  SWFShape sAlien1;
  SWFShape sAlien2;
  sAlien1 = newSWFShape(); /* blue color */
  sAlien2 = newSWFShape(); /* blue color */
  makeRect(sAlien1, 20,  15, -10,  -15, 0, 0, 255, 255); /* the body */
  makeRect(sAlien1, 5,   5,  -10,  0,   0, 0, 255, 255); /* the Left leg */
  makeRect(sAlien1, 5,   5,  5,    0,   0, 0, 255, 255); /* Right leg */
  makeRect(sAlien1, 3,   5, -5,   -10,  0, 0, 255, 255); /* Left eye */
  makeRect(sAlien1, 3,   5,  2,   -10,  0, 0, 255, 255); /* Right eye */

  makeRect(sAlien2, 20, 15, -10,  -15,  0, 0, 255, 255); /* Body */
  makeRect(sAlien2,  5,  5,  -7,    0,  0, 0, 255, 255); /* Left leg */
  makeRect(sAlien2,  5,  5,  2,     0,  0, 0, 255, 255); /* Right leg */
  makeRect(sAlien2,  3,  5,  -5,  -10,  0, 0, 255, 255); /* Left eye */
  makeRect(sAlien2,  3,  5,  2,   -10,  0, 0, 255, 255); /* Right eye */
  
  /* now create the photon bullet */
  SWFShape sPhotonBullet;
  sPhotonBullet = newSWFShape();
  makeRect(sPhotonBullet, 5, 10, -3, -10, 255, 255, 255, 255); /* white photon
bullet */

  /* now create the mouse click hit shape */
  SWFShape sMouseClickHit;
  sMouseClickHit = newSWFShape();
  makeRect(sMouseClickHit, 400, 400, 0, 0, 0, 0, 0, 255);

  /* newSWFSprite() I could have created a gun sprite but I decided to go
     with the movie clip jargon */
  /* now add the gun shape to a one frame long movie clip */
  SWFMovieClip myGunMovieClip;
  myGunMovieClip = newSWFMovieClip();
  gunDisplayItem myGunDisplayItem;
  myGunDisplayItem.i = SWFMovieClip_add(myGunMovieClip, sPlayerGun); 
  SWFDisplayItem_setName(myGunDisplayItem.i, "gun");
  SWFDisplayItem_moveTo(myGunDisplayItem.i, 200, 380);
  SWFMovieClip_nextFrame(myGunMovieClip);

  /* now create a four frame long movie clip.  2 for alien1 and 2 for alien2 */
  SWFMovieClip myAlienMovieClip;
  myAlienMovieClip = newSWFMovieClip();

  AlienDisplayItem myAlien1DisplayItem;
  AlienDisplayItem myAlien2DisplayItem;
  myAlien1DisplayItem.i = SWFMovieClip_add(myAlienMovieClip, sAlien1);
  SWFMovieClip_nextFrame(myAlienMovieClip);
  SWFMovieClip_nextFrame(myAlienMovieClip);
  SWFMovieClip_remove(myAlienMovieClip, myAlien1DisplayItem.i);
  myAlien2DisplayItem.i = SWFMovieClip_add(myAlienMovieClip, sAlien2);
  SWFMovieClip_nextFrame(myAlienMovieClip);
  SWFMovieClip_nextFrame(myAlienMovieClip);
  
  /* the bullet is another one frame long movie clip */
  SWFMovieClip myBulletMovieClip;
  myBulletMovieClip = newSWFMovieClip();

  BulletDisplayItem myBulletDisplayItem;
  myBulletDisplayItem.i = SWFMovieClip_add(myBulletMovieClip, sPhotonBullet);
  SWFDisplayItem_setName(myBulletDisplayItem.i, "bullet");
  SWFMovieClip_nextFrame(myBulletMovieClip);

  /* now create a text field for the game over message */
  SWFTextField t;
  SWFFont f;
  f = newSWFBrowserFont("_sans");
  t = newSWFTextField();
  SWFTextField_setFont(t, f);
  SWFTextField_setColor(t, 255,255,255, 255);
  SWFTextField_setHeight(t, 50);
  SWFTextField_setBounds(t, 300, 50);
  SWFTextField_setAlignment(t, SWFTEXTFIELD_ALIGN_CENTER);

  /* now create a movie called m */
  myMovie = newSWFMovie();

  /* set the movie m's dimension to 400, 400 */
  SWFMovie_setDimension(myMovie, 400, 400);

  /* set the movie m's background color to black */
  SWFMovie_setBackground(myMovie, 0x00, 0x00, 0x00);

  /* set the frames per second */
  SWFMovie_setRate(myMovie, 16);

  char firstFrameAction[255];
  sprintf(firstFrameAction, "direction = new Array( ); \
for (i=0; i<40; i++) { direction[i] = 1; } \
onScreenBullet = 0; \
");
  SWFMovie_add(myMovie, compileSWFActionCode(firstFrameAction));
  SWFMovie_nextFrame(myMovie);






  SWFButton myScreenAreaButton;
  myScreenAreaButton = newSWFButton();
  SWFButton_addShape(myScreenAreaButton, sMouseClickHit, SWFBUTTON_HIT);
  char screenAreaButtonAction[255];
  sprintf(screenAreaButtonAction, "if (onScreenBullet == 0) {\
 onScreenBullet = 1; \
 _root[\"bullet\"]._x = _root[\"gun\"]._x; \
_root[\"bullet\"]._y = _root[\"gun\"]._y; \
}");
  SWFButton_addAction(myScreenAreaButton,
compileSWFActionCode(screenAreaButtonAction), SWFBUTTON_MOUSEDOWN);

  ScreenButtonItem myScreenButtonItem;
  myScreenButtonItem.i = SWFMovie_add(myMovie, myScreenAreaButton);

  SWFDisplayItem gameOverMessageDisplayItem;
  gameOverMessageDisplayItem = SWFMovie_add(myMovie, t); 
  SWFDisplayItem_setName(gameOverMessageDisplayItem, "message");
  SWFDisplayItem_moveTo(gameOverMessageDisplayItem, 75, 100);

  /* add the player gun to the movie object m */
  SWFMovie_add(myMovie, myGunMovieClip);

  int tmpAlienRowCounter;
  int tmpAlienColumnCounter;
  tmpAlienColumnCounter = 0;
  tmpAlienRowCounter = 0;
  char tmpName[255];
  for(tmpAlienRowCounter = 0; tmpAlienRowCounter <= 4; tmpAlienRowCounter++)
    {
      for(tmpAlienColumnCounter = 0; tmpAlienColumnCounter <= 7;
tmpAlienColumnCounter++)
	{
	  SWFDisplayItem tmpAlienDisplayItem;
	  tmpAlienDisplayItem = SWFMovie_add(myMovie, myAlienMovieClip);
	  SWFDisplayItem_moveTo(tmpAlienDisplayItem, tmpAlienRowCounter*40,
tmpAlienColumnCounter*40);
	  sprintf(tmpName, "Alien%d", (tmpAlienRowCounter*8 + tmpAlienColumnCounter));
	  SWFDisplayItem_setName(tmpAlienDisplayItem, tmpName);
	}
    }

  /* add the bullet to the stage */
  SWFDisplayItem myBulletMovieDisplayItem;
  myBulletMovieDisplayItem = SWFMovie_add(myMovie, myBulletMovieClip);
  SWFDisplayItem_moveTo(myBulletMovieDisplayItem,-10, -10);
  SWFDisplayItem_setName(myBulletMovieDisplayItem, "bullet");
  SWFMovie_nextFrame(myMovie);

  /*
    The movement of the gun, aliens, and bullet 
    are all controlled by the following bit of ActionScript 
    that is executed every time Frame 3 is executed. 
    The script 
    -moves the players gun, 
    -moves each of the aliens, 
    -checks for a collision with the bullet, 
    -checks for a collision with the edge of the screen, 
    -checks for a collision with the bottom of the screen, 
    -moves the bullet
  */



  char thirdFrameAction[4000];
  sprintf(thirdFrameAction, "\
/* Move the gun to follow the mouse. Note that the gun moves faster the farther
it is from the mouse */ \
  dx = int(_xmouse - _root[\"gun\"]._x)/10;\
  xPos = _root[\"gun\"]._x + dx;\
  if ((xPos > 0) && (xPos < 400)) { _root[\"gun\"]._x += dx; } \
  \
  /* Move each of the aliens */ \
  for (i=0; i<40; i++) \
  { \ 
     /* If an alien reaches the bottom, end the game */ \
     if (_root[\"alien\"+i]._y > 380) \
     { message = \"Game Over\"; stop( ); } \
    \
     /* If an alien hits one of the margins, reverse direction */\
     if (_root[\"alien\"+i]._x > 380) \
     { \
        direction[i] = -1; \
        _root[\"alien\"+i]._y += 20; \
     } \
     if (_root[\"alien\"+i]._x < 20) \
     { \
         direction[i] = 1; \
         _root[\"alien\"+i]._y += 20; \
     } \
     \
     /* Move the alien */ \
     _root[\"alien\"+i]._x += direction[i] * 5; \
     /* Check to see if the bullet has collided with the alien \
     If so, move the bullet and alien off screen */ \
     if (onScreenBullet & _root[\"bullet\"].hitTest(_root[\"alien\"+i])) \
     { \
        _root[\"bullet\"]._y = -10; \
        _root[\"alien\"+i]._y = -10; \
        onScreenBullet = 0; \
        direction[i] = 0; \
     } \
  } 
\
  /* If the bullet is on the screen, move it upward. */ \
  if (onScreenBullet) \
  { \
     _root[\"bullet\"]._y -= 10; \
     if (_root[\"bullet\"]._y < 0) { onScreenBullet = 0; } \
  } \
\
");
  SWFMovie_add(myMovie, compileSWFActionCode(thirdFrameAction));
  SWFMovie_nextFrame(myMovie);


  /* for the fourth frame just go back to the previous frame and then start
playing */
  char fourthFrameAction[255];
  sprintf(fourthFrameAction, "\
  prevFrame(); \
  play(); \
\
");
  SWFMovie_add(myMovie, compileSWFActionCode(fourthFrameAction));
  /* at this point it doesn't make sense saying next frame but it's needed
anyways. */
  SWFMovie_nextFrame(myMovie);
  
  /* now write the flash output file .swf */
  SWFMovie_save(myMovie, "astral.swf");

  exit(0);
}