Wednesday, 28 November 2012

How to bounce a ball using arrow keys


//These variables will note which keys are down
//We don't need the up or down key just yet
//but we will later
var leftKeyDown:Boolean = false;
var upKeyDown:Boolean = false;
var rightKeyDown:Boolean = false;
var downKeyDown:Boolean = false;
//the main character's speed
var mainSpeed:Number = 7;
//whether or not the main guy is jumping
var mainJumping:Boolean = false;
//how quickly should the jump start off
var jumpSpeedLimit:int = 15;
//the current speed of the jump;
var jumpSpeed:Number = 0;

//adding a listener to mcMain which will make it move
//based on the key strokes that are down
mcMain.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event:Event):void{
    //if certain keys are down then move the character
    if(leftKeyDown){
        mcMain.x -= mainSpeed;
    }
    if(rightKeyDown){
        mcMain.x += mainSpeed;
    }
    if(upKeyDown || mainJumping){
        mainJump();
    }
}

//listening for the keystrokes
//this listener will listen for down keystrokes
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
function checkKeysDown(event:KeyboardEvent):void{
    //making the booleans true based on the keycode
    //WASD Keys or arrow keys
    if(event.keyCode == 37 || event.keyCode == 65){
        leftKeyDown = true;
    }
    if(event.keyCode == 38 || event.keyCode == 87){
        upKeyDown = true;
    }
    if(event.keyCode == 39 || event.keyCode == 68){
        rightKeyDown = true;
    }
    if(event.keyCode == 40 || event.keyCode == 83){
        downKeyDown = true;
    }
}
//this listener will listen for keys being released
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
function checkKeysUp(event:KeyboardEvent):void{
    //making the booleans false based on the keycode
    if(event.keyCode == 37 || event.keyCode == 65){
        leftKeyDown = false;
    }
    if(event.keyCode == 38 || event.keyCode == 87){
        upKeyDown = false;
    }
    if(event.keyCode == 39 || event.keyCode == 68){
        rightKeyDown = false;
    }
    if(event.keyCode == 40 || event.keyCode == 83){
        downKeyDown = false;
    }
}

//jumping function
function mainJump():void{
    //if main isn't already jumping
    if(!mainJumping){
        //then start jumping
        mainJumping = true;
        jumpSpeed = jumpSpeedLimit*-1;
        mcMain.y += jumpSpeed;
    } else {
        //then continue jumping if already in the air
        //crazy math that I won't explain
        if(jumpSpeed < 0){
            jumpSpeed *= 1 - jumpSpeedLimit/75;
            if(jumpSpeed > -jumpSpeedLimit/5){
                jumpSpeed *= -1;
            }
        }
        if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){
            jumpSpeed *= 1 + jumpSpeedLimit/50;
        }
        mcMain.y += jumpSpeed;
        //if main hits the floor, then stop jumping
        //of course, we'll change this once we create the level
        if(mcMain.y >= stage.stageHeight - mcMain.height){
            mainJumping = false;
            mcMain.y = stage.stageHeight - mcMain.height;
        }
    }
}





  • Create a movie clip with name of ‘mcMain’ (use at above code)
  • Copy and paste code at action panel.
  • Press Ctrl+Enter to check output.

How to Bouncing or falling a ball in action script 3.0



var ball:Sprite = new Sprite();
ball.graphics.beginFill(0);
ball.graphics.drawCircle(0, 0, 10);
ball.x = 275;
ball.y = 390;
addChild(ball);

var xPos:Number = ball.x;
var yPos:Number = ball.y;
var xVel:Number = (Math.random() * 10) - 5;
var yVel:Number = (Math.random() * -10) - 10;
var grav:Number = 1;

addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);
function onLoop(evt:Event):void
{
    yVel +=  grav;
    xPos +=  xVel;
    yPos +=  yVel;

    if (yPos > 390)
    {
        yPos = 390;
        yVel *=  -.7;
        xVel *=  .7;
    }

    ball.x = xPos;
    ball.y = yPos;
}

How to move Clouds in action script 3.0



var value:Number = 0.0;

addEventListener( Event.ENTER_FRAME, onEnterFrame );

function onEnterFrame( e:Event ):void
{
                cloud.y = 40 + ( Math.sin( value ) * 4 );
                value +=  0.1;
}



  • Create a movie clip named as ‘cloud’.

  •  Copy and paste above code on your actionscript panel.

  • Press Ctrl+Enter and check cloud movement in output panel

How to export or publish GIF image in CS5










































Arrays of Objects



One of the most useful structures in Flash --especially when dealing with xml or database table data and list-type components like ComboBox and DataGrid-- is an array of objects. Because you can define any number of 'fields' as the properties of an object, and any number of 'records' in an array, the two make a good recordset-like combination.
The following code, for example, creates an array of objects that is sorted alphabetically by title and used as the data provider for a combobox component. The combobox (instancename bookchoices) displays a list of book titles. Each entry (element of the array) is associated with other data (the other properties of the object in that element) which could be used to populate an order form, saved in a database, or emailed to an interested party when selected from the dropdown by a site visitor:
var books:Array = [
        {title:"Flash book 1", author:"abc", pubyr:2001},
        {title:"Flash book 2", author:"def", pubyr:2000},
        {title:"Flash book 3", author:"ghi", pubyr:2001},
        {title:"Flash book 4", author:"Robert Penner", pubyr:2002}
        ];

books.sortOn("title");
bookchoices.labelField = "title";
bookchoices.dataProvider = books;

Accessing properties of an object



There are two ways to access a property of an object. One way is with dot syntax:
var booktitle:String = eas2book.title;
and the other way is with a string index (treating the object as what's called an associative array):
var booktitle:String = eas2book["title"];
The former is more compact; the latter is useful when properties are to be accessed with a variable representation or in a loop, eg:
var field:String = "title";
var booktitle:String = eas2book[field];
trace(booktitle);  // Essential Actionscript 2.0
I'll repeat that because it's important to remember:
Accessing properties of an object

There are two ways to access a property in an object. One is with dot syntax:

eas2book.title;

and the other is with a string index:

eas2book["title"];