Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Need help starting out in Flash CS3
New Posts  All Forums:Forum Nav:

Need help starting out in Flash CS3 - Page 2

post #11 of 19
Click on the frame symbol on the timeline to be able to write the code. Don't select the box.
über pwnage v2
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K 4.4GHz @ 1.36v Asus P8Z68-V LX EVGA GTX 970 SuperClocked ACX2.0 4GB G.SKILL Ripjaws X 2x8GB DDR3 @ 1648MHz 
Hard DriveOptical DriveCoolingOS
840EVO 250GB / 3TB+2TB+320GB Bar / 750GB Cav Blk Samsung DVDRW Hyper 212+ Windows 10 Pro x64 
MonitorKeyboardPowerCase
U2412M + X191W Azio MGK1 Corsair RM650X Centurion 5 with acoustic foam 
MouseMouse PadAudio
MX518 SteelSeries Xonar DG + Koss PortaPro + Logitech X-540 + Bey... 
  hide details  
Reply
über pwnage v2
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K 4.4GHz @ 1.36v Asus P8Z68-V LX EVGA GTX 970 SuperClocked ACX2.0 4GB G.SKILL Ripjaws X 2x8GB DDR3 @ 1648MHz 
Hard DriveOptical DriveCoolingOS
840EVO 250GB / 3TB+2TB+320GB Bar / 750GB Cav Blk Samsung DVDRW Hyper 212+ Windows 10 Pro x64 
MonitorKeyboardPowerCase
U2412M + X191W Azio MGK1 Corsair RM650X Centurion 5 with acoustic foam 
MouseMouse PadAudio
MX518 SteelSeries Xonar DG + Koss PortaPro + Logitech X-540 + Bey... 
  hide details  
Reply
post #12 of 19
Thread Starter 
So if I have a Movie Clip symbol "Ball" with instance "Ballstance" for example, how would I control movement of "Ball" with the arrow keys?

I would guess using a command to draw "ball" at coordinates "x,y", when Key.LEFT is pressed X=X-1 (not sure how to format it for AS3?), Key.Right is pressed than X=X+1, and so on (Up+Down edit Y)


Code:
//(Taken from an online tutorial)

onClipEvent (enterFrame) {
       if (Key.isDown(Key.LEFT)){
             _x-=3;
       }
       if (Key.isDown(Key.RIGHT)){
             _x+=3;
       }
}
Sager NP5165
(7 items)
 
 
CPUGraphicsHard DriveOptical Drive
I7 2630QM Nvidia Geforce GT 555 Hitachi BLU-RAY 
MonitorMonitorMouse
1080p Matte 95% Color Gamut Yamakasi Precision 2703 LED IPS Microsoft Sidewinder 
CPUMotherboardGraphicsRAM
Opteron 170 DFI NF4 Ultra-D 8800GTS 4x1GB GSkill 500mhz HZs 
Hard DriveOptical DriveOSMonitor
160GB + 120GB LiteOn LiteScribe DVD burner Windows 7 Home Prem. Samsung 220WM 22" + LG f-Engine 17" (dual screen) 
KeyboardPowerCaseMouse
HP Internet/Media Keyboard Ultra 600W AeroCool AeroEngine II. Rice for computers Logitech LX7 
Mouse Pad
Belkin with Gel Wrist Pad 
  hide details  
Reply
Sager NP5165
(7 items)
 
 
CPUGraphicsHard DriveOptical Drive
I7 2630QM Nvidia Geforce GT 555 Hitachi BLU-RAY 
MonitorMonitorMouse
1080p Matte 95% Color Gamut Yamakasi Precision 2703 LED IPS Microsoft Sidewinder 
CPUMotherboardGraphicsRAM
Opteron 170 DFI NF4 Ultra-D 8800GTS 4x1GB GSkill 500mhz HZs 
Hard DriveOptical DriveOSMonitor
160GB + 120GB LiteOn LiteScribe DVD burner Windows 7 Home Prem. Samsung 220WM 22" + LG f-Engine 17" (dual screen) 
KeyboardPowerCaseMouse
HP Internet/Media Keyboard Ultra 600W AeroCool AeroEngine II. Rice for computers Logitech LX7 
Mouse Pad
Belkin with Gel Wrist Pad 
  hide details  
Reply
post #13 of 19
Quote:
Originally Posted by nategr8ns View Post
So if I have a Movie Clip symbol "Ball" with instance "Ballstance" for example, how would I control movement of "Ball" with the arrow keys?
onClipEvent is not supported in AS3, that's an AS2 keyword. There are a number of ways to approach trapping arrow keys and performing movement. Here's one:

Code:
var keyMap:Object = new Object();
var PIXELS_PER_SECOND:int = 200;
var PIXELS_PER_FRAME:Number = PIXELS_PER_SECOND / stage.frameRate;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyWasPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyWasReleased);
stage.addEventListener(Event.ENTER_FRAME, frameWasEntered);

function keyWasPressed(evt:KeyboardEvent): void
{
keyMap[evt.keyCode] = true;
}

function keyWasReleased(evt:KeyboardEvent): void
{
keyMap[evt.keyCode] = false;
}

function frameWasEntered(evt:Event): void
{
if(keyMap[Keyboard.LEFT])
Ballstance.x -= PIXELS_PER_FRAME;
if(keyMap[Keyboard.RIGHT])
Ballstance.x += PIXELS_PER_FRAME;
if(keyMap[Keyboard.UP])
Ballstance.y -= PIXELS_PER_FRAME;
if(keyMap[Keyboard.DOWN])
Ballstance.y += PIXELS_PER_FRAME;
}
This code stores key presses in a key map object, and then when the frame is updated, adjusts the position of the clip based on which keys are currently down. With this method you can hold down two keys to go diagonally. The ball instance will travel 200 pixels per second, regardless of what your frame rate is set to. Your frame rate can be changed by clicking on a blank part of the stage and looking at Frame Rate on the Properties panel. It defaults to 12. You'll notice that although the ball doesn't move any faster, the animation gets smoother as you increase the frame rate. To actually change the speed of the ball movement, change the value 200 to something else.

For consistency, I suggest you name your Library symbols "LikeThisHere" and your instances "likeThisHere", so you can tell them apart at a glance.
    
CPUGraphicsRAMHard Drive
Intel 2.4 Core i7 AMD Radeon HD 6750M 8 GB 1067 MHz DDR3 750 GB 
OS
Mac OS-X Lion 
  hide details  
Reply
    
CPUGraphicsRAMHard Drive
Intel 2.4 Core i7 AMD Radeon HD 6750M 8 GB 1067 MHz DDR3 750 GB 
OS
Mac OS-X Lion 
  hide details  
Reply
post #14 of 19
Thread Starter 
Code:
var keyMap:Object = new Object();
var PIXELS_PER_SECOND:int = 200;
var PIXELS_PER_FRAME:Number = PIXELS_PER_SECOND / stage.frameRate;
Declaring variables, pixels_per_second is speed of motion and pixels_per_frame is used to keep speed the same no matter what the framerate is, cool.

Code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyWasPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyWasReleased);
stage.addEventListener(Event.ENTER_FRAME, frameWasEntered);
not sure what this is

Code:
function keyWasPressed(evt:KeyboardEvent): void
{
keyMap[evt.keyCode] = true;
}

function keyWasReleased(evt:KeyboardEvent): void
{
keyMap[evt.keyCode] = false;
}
these just "Getkey" as I'm used to in TI-BASIC and when called on later in frameWasEntered just return true or false.

Code:
function frameWasEntered(evt:Event): void
{
if(keyMap[Keyboard.LEFT])
INSTANCE.x -= PIXELS_PER_FRAME;
if(keyMap[Keyboard.RIGHT])
INSTANCE.x += PIXELS_PER_FRAME;
if(keyMap[Keyboard.UP])
INSTANCE.y -= PIXELS_PER_FRAME;
if(keyMap[Keyboard.DOWN])
INSTANCE.y += PIXELS_PER_FRAME;
}
That part was easy, just adjusted coordinates of an instance (I replaced ballstance with instance because it could be any instance)
so...
the Instance (x or y) is (decreasing/increasing) at speed of PIXELS_PER_FRAME

so if I wanted to move "cube" up 5, I would go:
if argument is true blahblahblah
cube.y += 5

correct?
Sager NP5165
(7 items)
 
 
CPUGraphicsHard DriveOptical Drive
I7 2630QM Nvidia Geforce GT 555 Hitachi BLU-RAY 
MonitorMonitorMouse
1080p Matte 95% Color Gamut Yamakasi Precision 2703 LED IPS Microsoft Sidewinder 
CPUMotherboardGraphicsRAM
Opteron 170 DFI NF4 Ultra-D 8800GTS 4x1GB GSkill 500mhz HZs 
Hard DriveOptical DriveOSMonitor
160GB + 120GB LiteOn LiteScribe DVD burner Windows 7 Home Prem. Samsung 220WM 22" + LG f-Engine 17" (dual screen) 
KeyboardPowerCaseMouse
HP Internet/Media Keyboard Ultra 600W AeroCool AeroEngine II. Rice for computers Logitech LX7 
Mouse Pad
Belkin with Gel Wrist Pad 
  hide details  
Reply
Sager NP5165
(7 items)
 
 
CPUGraphicsHard DriveOptical Drive
I7 2630QM Nvidia Geforce GT 555 Hitachi BLU-RAY 
MonitorMonitorMouse
1080p Matte 95% Color Gamut Yamakasi Precision 2703 LED IPS Microsoft Sidewinder 
CPUMotherboardGraphicsRAM
Opteron 170 DFI NF4 Ultra-D 8800GTS 4x1GB GSkill 500mhz HZs 
Hard DriveOptical DriveOSMonitor
160GB + 120GB LiteOn LiteScribe DVD burner Windows 7 Home Prem. Samsung 220WM 22" + LG f-Engine 17" (dual screen) 
KeyboardPowerCaseMouse
HP Internet/Media Keyboard Ultra 600W AeroCool AeroEngine II. Rice for computers Logitech LX7 
Mouse Pad
Belkin with Gel Wrist Pad 
  hide details  
Reply
post #15 of 19
Quote:
Originally Posted by nategr8ns View Post
Code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyWasPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyWasReleased);
stage.addEventListener(Event.ENTER_FRAME, frameWasEntered);
not sure what this is
AS3 is heavily event driven. All movie clips, including the "root" movie clip have a stage property that points to the same instance of a Stage object. From here you can access the frame rate, check out the dimensions of the playing area, or add generic event handlers. In this case, we're adding three. One for when a key is depressed (KEY_DOWN), one for when a key is lifted (KEY_UP) and one for the when the frame is updated (ENTER_FRAME). The first parameter is the type of event to listen for, and the second parameter is the function to call when the event happens. keyWasPressed, keyWasReleased, and frameWasEntered are the parameters here, and also (not coincidentally) the functions that are declared at the bottom of the code. If you don't register your event handlers with addEventListener, the functions will not get called.

Quote:
Originally Posted by nategr8ns View Post
these just "Getkey" as I'm used to in TI-BASIC and when called on later in frameWasEntered just return true or false.
Exactly, except I imagine GetKey in TI-BASIC actually paused the program to wait for a keystroke, where as the event listeners don't cause anything to pause. The application keeps on running and doing whatever it needs, and is interrupted when events occur.

AS2 has a function for querying whether or not a specific key is down, but this was removed in AS3 in favor of a more strict event model.

Quote:
Originally Posted by nategr8ns View Post
That part was easy, just adjusted coordinates of an instance (I replaced ballstance with instance because it could be any instance)
Well, this code works specifically on a single SPECIFICALLY NAMED instance. It doesn't make sense to call it INSTANCE. You should call it ball, or tank, or player, whatever it actually is.

Quote:
Originally Posted by nategr8ns View Post
so...
the Instance (x or y) is (decreasing/increasing) at speed of PIXELS_PER_FRAME
Yes, at every frame it changes by that # of pixels.

Quote:
Originally Posted by nategr8ns View Post
so if I wanted to move "cube" up 5, I would go:
if argument is true blahblahblah
cube.y += 5

correct?
The other way around. 0,0 is the upper left hand corner. So X increases to the right, and Y increases to the bottom. Subtract 5 to go up.
    
CPUGraphicsRAMHard Drive
Intel 2.4 Core i7 AMD Radeon HD 6750M 8 GB 1067 MHz DDR3 750 GB 
OS
Mac OS-X Lion 
  hide details  
Reply
    
CPUGraphicsRAMHard Drive
Intel 2.4 Core i7 AMD Radeon HD 6750M 8 GB 1067 MHz DDR3 750 GB 
OS
Mac OS-X Lion 
  hide details  
Reply
post #16 of 19
Thread Starter 
Quote:
Originally Posted by Scriptorum View Post
Well, this code works specifically on a single SPECIFICALLY NAMED instance. It doesn't make sense to call it INSTANCE. You should call it ball, or tank, or player, whatever it actually is.



Yes, at every frame it changes by that # of pixels.



The other way around. 0,0 is the upper left hand corner. So X increases to the right, and Y increases to the bottom. Subtract 5 to go up.
actually, getKey did not wait. Say you want to show a picture, and when the user presses a button (we'll say the "down" button aka button 34) you would have to make a loop just for it.

repeat K=34 //repeats until K=34
getKey>K //stores value returned by getKey as variable K
End //end of loop, returns to beginning of loop if K =/= 34

I was just saying INSTANCE because it didn't have to be a specific instance, it could be my tank instance or ball or whatever.

oh yeah, I forgot that computers started at the top right
Sager NP5165
(7 items)
 
 
CPUGraphicsHard DriveOptical Drive
I7 2630QM Nvidia Geforce GT 555 Hitachi BLU-RAY 
MonitorMonitorMouse
1080p Matte 95% Color Gamut Yamakasi Precision 2703 LED IPS Microsoft Sidewinder 
CPUMotherboardGraphicsRAM
Opteron 170 DFI NF4 Ultra-D 8800GTS 4x1GB GSkill 500mhz HZs 
Hard DriveOptical DriveOSMonitor
160GB + 120GB LiteOn LiteScribe DVD burner Windows 7 Home Prem. Samsung 220WM 22" + LG f-Engine 17" (dual screen) 
KeyboardPowerCaseMouse
HP Internet/Media Keyboard Ultra 600W AeroCool AeroEngine II. Rice for computers Logitech LX7 
Mouse Pad
Belkin with Gel Wrist Pad 
  hide details  
Reply
Sager NP5165
(7 items)
 
 
CPUGraphicsHard DriveOptical Drive
I7 2630QM Nvidia Geforce GT 555 Hitachi BLU-RAY 
MonitorMonitorMouse
1080p Matte 95% Color Gamut Yamakasi Precision 2703 LED IPS Microsoft Sidewinder 
CPUMotherboardGraphicsRAM
Opteron 170 DFI NF4 Ultra-D 8800GTS 4x1GB GSkill 500mhz HZs 
Hard DriveOptical DriveOSMonitor
160GB + 120GB LiteOn LiteScribe DVD burner Windows 7 Home Prem. Samsung 220WM 22" + LG f-Engine 17" (dual screen) 
KeyboardPowerCaseMouse
HP Internet/Media Keyboard Ultra 600W AeroCool AeroEngine II. Rice for computers Logitech LX7 
Mouse Pad
Belkin with Gel Wrist Pad 
  hide details  
Reply
post #17 of 19
Quote:
Originally Posted by nategr8ns View Post
actually, getKey did not wait. Say you want to show a picture, and when the user presses a button (we'll say the "down" button aka button 34) you would have to make a loop just for it.
Ah, ok. This is similar to the functionality in AS2. But in AS3 you have to set up event handlers to track key presses, which is probably was AS2 was doing behind the scenes anyhow.

Quote:
Originally Posted by nategr8ns View Post
I was just saying INSTANCE because it didn't have to be a specific instance, it could be my tank instance or ball or whatever.
I see. Sure. Generally you're only going to be controlling one thing at a time, but it's possible you could make a game where you have different things to control. In that case you'd probably track the current movie clip with a variable:

Code:
var playerMC:MovieClip;
var playerSpeed:Number; 

function setPlayer(clip:MovieClip, speed:Number): void
{
    playerMC = clip;
    playerSpeed = speed / stage.frameRate;
}
And then the subsequent frameWasEntered function would refer to playerMC and playerSpeed instead of INSTANCE and PIXELS_PER_FRAME to determine what to move and how to move it. You could then call setPlayer(Ballstance,200); to allow the keys to move the Ballstance clip at 200 pixels per second. When you wanted to switch control to another object, make another call to setPlayer.

Quote:
Originally Posted by nategr8ns View Post
oh yeah, I forgot that computers started at the top right
You mean upper left.

There's nothing to say a graphical framework couldn't use a different corner as 0,0, or swap direction of the axes. The orientation of the Cartesian system is pretty arbitrary, but yeah, upper left is common. I suppose it's because we write that way in western languages.
    
CPUGraphicsRAMHard Drive
Intel 2.4 Core i7 AMD Radeon HD 6750M 8 GB 1067 MHz DDR3 750 GB 
OS
Mac OS-X Lion 
  hide details  
Reply
    
CPUGraphicsRAMHard Drive
Intel 2.4 Core i7 AMD Radeon HD 6750M 8 GB 1067 MHz DDR3 750 GB 
OS
Mac OS-X Lion 
  hide details  
Reply
post #18 of 19
Always check the webmonkey. (beginner flash tutorial)
Craiginator
(13 items)
 
  
CPUMotherboardGraphicsRAM
e6420 - L712a517 Abit AB9 Pro P965 XFX 7800 GTX 2GB GEIL PC2-8500 Ultra 
Hard DriveOptical DriveOSMonitor
2x Wd 160gb Raid 0 Asus Dvd/Cd-RW Ubuntu / XPlite 22' Acer ws X222W 
KeyboardPowerCaseMouse
G15 Antec 550 True Power Antec 900 G5 
Mouse Pad
Yes Please. 
  hide details  
Reply
Craiginator
(13 items)
 
  
CPUMotherboardGraphicsRAM
e6420 - L712a517 Abit AB9 Pro P965 XFX 7800 GTX 2GB GEIL PC2-8500 Ultra 
Hard DriveOptical DriveOSMonitor
2x Wd 160gb Raid 0 Asus Dvd/Cd-RW Ubuntu / XPlite 22' Acer ws X222W 
KeyboardPowerCaseMouse
G15 Antec 550 True Power Antec 900 G5 
Mouse Pad
Yes Please. 
  hide details  
Reply
post #19 of 19
Thread Starter 
double , I meant upper left
thanks diagnosis, I'll check that out
Sager NP5165
(7 items)
 
 
CPUGraphicsHard DriveOptical Drive
I7 2630QM Nvidia Geforce GT 555 Hitachi BLU-RAY 
MonitorMonitorMouse
1080p Matte 95% Color Gamut Yamakasi Precision 2703 LED IPS Microsoft Sidewinder 
CPUMotherboardGraphicsRAM
Opteron 170 DFI NF4 Ultra-D 8800GTS 4x1GB GSkill 500mhz HZs 
Hard DriveOptical DriveOSMonitor
160GB + 120GB LiteOn LiteScribe DVD burner Windows 7 Home Prem. Samsung 220WM 22" + LG f-Engine 17" (dual screen) 
KeyboardPowerCaseMouse
HP Internet/Media Keyboard Ultra 600W AeroCool AeroEngine II. Rice for computers Logitech LX7 
Mouse Pad
Belkin with Gel Wrist Pad 
  hide details  
Reply
Sager NP5165
(7 items)
 
 
CPUGraphicsHard DriveOptical Drive
I7 2630QM Nvidia Geforce GT 555 Hitachi BLU-RAY 
MonitorMonitorMouse
1080p Matte 95% Color Gamut Yamakasi Precision 2703 LED IPS Microsoft Sidewinder 
CPUMotherboardGraphicsRAM
Opteron 170 DFI NF4 Ultra-D 8800GTS 4x1GB GSkill 500mhz HZs 
Hard DriveOptical DriveOSMonitor
160GB + 120GB LiteOn LiteScribe DVD burner Windows 7 Home Prem. Samsung 220WM 22" + LG f-Engine 17" (dual screen) 
KeyboardPowerCaseMouse
HP Internet/Media Keyboard Ultra 600W AeroCool AeroEngine II. Rice for computers Logitech LX7 
Mouse Pad
Belkin with Gel Wrist Pad 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Need help starting out in Flash CS3