Bits & Bytes

Posts Tagged ‘web’

Creating a Simple Animation in Actionscript 3.0

In this example, I create a red rectangle the moves back and forth across the screen. The primary purpose of this example is to show how to create a callback function for animation. So, I will not bother to explain too much about the coordinate calculations that are used for motion.

To create an animation, I could use a Timer object to set an time interval for the animation speed. However, it is better to use the frame rate of the application for controlling the animation. In this manner, the speed of the animation can be altered via the “Properties” window, and should be set to a level that is appropriate for the complexity of the program. The typical setting that is currently used for Flash is 24 frames per second. In order to set a callback that is called every frame, we can use the event ENTER_FRAME, as shown below.

addEventListener(Event.ENTER_FRAME, NextFrame);

// Create rect for sprite and add to stage
var qRect:Sprite = new Sprite();
var uiColor:uint = 0x00c00000;
qRect.graphics.beginFill(uiColor);
    qRect.graphics.drawRect(0, 0, 100, 75);
qRect.graphics.endFill();
addChild(qRect);

// Variables for motion control
var uiX:uint = 0;
var uiDeltaX:uint = 4;
var kuiMaxX:uint = 220;

// Animation callback
function NextFrame(e:Event) : void {
	uiX = ((uiX + uiDeltaX) % (kuiMaxX + 1));
	qRect.x = uiX;
	if (uiX == kuiMaxX || uiX == 0) {
		uiDeltaX = (kuiMaxX - uiDeltaX);
	}
}

Here, we have the code to move a rectangle back and forth across the screen. The first line of code sets the function NextFrame() as a callback that responds to the event that is generated at the start of a frame. After this, we have some code that creates a Sprite, draws a red rectangle on it, and adds the Sprite to the stage so that it will be displayed. In the next, three lines, we initialize the variables that will control the motion of the rectangle. Finally, we define the callback function, NextFrame(), which may seem confusing. We want to focus on the fact that this function sets the x-coordinate of the Sprite, qRect.x, to control the motion, while the rest of the code is just used for calculating the value of x.

Declaring Variables and Constants in Actionscript with Fundamental Types

The basic format for declaring variables in actionscript looks like this:

var <variable name>:<variable type>;

We begin with var to indicate that we are declaring a variable. Then, we put a space, the variable name, a colon, the variable type, and, finally, a semi-colon to end the line. So, for example, we could declare a variable named MyNum of the type Number like this:

var dMyNum:Number;

A Number is used for floating-point or decimal numbers. We can initialize our variables in the declaration like this:

var dMyNum:Number = 3.14159;

Additionally, we can declare constants, which should be initialized, like this:

const kdMyNum:Number = 3.14159;

Actionscript has few a fundamental types to hold common data elements. These types are String, Boolean, Number, int, and uint. Strings are used to hold text. Booleans are used to hold the logical values “true” or “false”. Numbers are used to hold any number, including non-integer numbers. ints are used to hold integers only. uints are used to hold unsigned integers only. An example declaration and initialization of each of these types is shown here:

var sMyString:String = "XoaX.net";
var bMyBool:Boolean = true;
var dMyNum:Number = 3.14159;
var iMyInt:int = -67;
var uiMyUint:uint = 34;

Our first variable declaration did not contain an initial value. Variables that are declared with no initial value assigned to them take the following default values, according to their type:

String: null
Boolean: false
Number: NaN or not a number
int: 0
uint: 0

PHP Cookies, Domains, and Subdomains (oh my!)

Programming in PHP on the web is always interesting, and working with cookies is no exception. For instance, you can set a cookie like this:

setcookie(“a”, “123”);

where “a” is the name and “123” is the value of the cookie.  Then you would probably expect it to be available wherever  you go in your domain, which we’ll call http://example.com. (Note: do not expect your cookie information to be available if you go to a page on another domain, such as http://myothersite.com). However, what happens when you have a link on your page that takes you to one of your subdomains, like http://subsite.example.com? The page in the subdomain needs the cookie you set, but when you arrive at the new page, the cookie is empty. What happened?

Even though crossing from http://example.com to http://subsite.example.com may not seem like much, it is to a cookie. Cookies are not, by default, available across the subdomains of your domain.

So, when you are on http://example.com, you can set your cookie with name “a” and value “123” and expect it to be available on any page that has http://example.com as part of its URL. However, if you want your cookie to be available as well on http://subsite.example.com, or on all of your subdomains and in any of your directories on your domain, you will need to add a few parameters to the call to setcookie(), like this:

setcookie(“a”, “123”, 0, “/”, “.example.com”);

  1. “a” is the name of the cookie
  2. “123” is the value of the cookie
  3. 0 is the time of expiration. The default is 0, which means the cookie will expire at the end of the session, which is when the browser closes.
  4. “/” refers to where this cookie works. Setting it to “/” will make it available over the entire domain specified in #5. Otherwise, it will be set for the directory that your script is in, which may not be desired.
  5. “.example.com” is the domain over which this cookie remains valid. Adding the “.” to the front of “example.com” will keep the cookie valid over all subdomains of the domain. Do not put “www” in this parameter unless you want the cookie to be valid only over the domain “www.example.com”. If you do, any page with an “example.com” URL then will not have access to the cookie.

Always remember: you must set a cookie before printing any output to the browser.

This function call will return true if the cookie was successfully set, false otherwise.

 

© 2007–2026 XoaX.net LLC. All rights reserved.