Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 06-30-08   #11 (permalink)
PC Gamer
 
Boris4ka's Avatar
 
intel nvidia

Join Date: Mar 2006
Location: San Francisco, CA
Posts: 2,829
Blog Entries: 2

Rep: 194 Boris4ka is acknowledged by manyBoris4ka is acknowledged by many
Unique Rep: 156
Trader Rating: 17
Default

Click on the frame symbol on the timeline to be able to write the code. Don't select the box.

System: über pwnage
CPU
E6600 3.0GHz 1.4v
Motherboard
Gigabyte GA-P35-S3L
Memory
2x2GB G.Skill 6400 835MHz
Graphics Card
EVGA 8800GTS 320MB
Hard Drive
320GB .10 + 80GB .7
Sound Card
Onboard HD Audio
Power Supply
Rosewill Performance 550W
Case
Centurion 5 w/ side window
CPU cooling
Golden Orb 2 - lapped
GPU cooling
Stock fan
OS
Vista Ultimate x64
Monitor
X191W 19"w + Sony 15" LCDs
Boris4ka is offline Boris4ka's Gallery   Reply With Quote
Old 06-30-08   #12 (permalink)
Commodore 64
 
nategr8ns's Avatar
 
amd nvidia

Join Date: Dec 2007
Location: Maine
Posts: 4,278

Rep: 130 nategr8ns is acknowledged by manynategr8ns is acknowledged by many
Unique Rep: 109
Folding Team Rank: 241
Trader Rating: 3
Default

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;
       }
}

System: OptyMSI Prime (soon "DFO: Designed for Optyvation"
CPU
Opteron 175 LCB9E 0652WPMW
Motherboard
MSI K8N Neo4-f (DFI NF4 Ultra-D en-closet)
Memory
2x1GB GSkill 500mhz HZs
Graphics Card
EVGA 8800GT SC 512
Hard Drive
160GB + 120GB
Sound Card
onboard ftw!
Power Supply
Ultra X-Finity 600w
Case
AeroCool AeroEngine II <3
CPU cooling
ACFreezer Pro w/92mm+80mm+120mm
GPU cooling
AC Accelero S1 w/Dual Tri-Cool 120mm (low)
OS
XP Home + Vista Ultimate after Mobo upgrade
Monitor
Samsung 220WM 22" + LG f-Engine 17" (dual screen)
nategr8ns is offline I fold for Overclock.net   Reply With Quote
Old 06-30-08   #13 (permalink)
Chiefly Ignorant
 
Scriptorum's Avatar
 
intel nvidia

Join Date: Jan 2008
Location: Atlanta, GA
Posts: 54
Blog Entries: 12

Rep: 13 Scriptorum Unknown
Unique Rep: 10
Trader Rating: 0
Default

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.
__________________
Quote:
Originally Posted by The Bartender Paradox View Post
crazy?...nah. when you cool with a fire extinguisher then your crazy.

System: Flaming Moe's MacBook Pro
CPU
Core 2 Duo
Motherboard
MacBook Pro
Memory
4GB DDR2-667
Graphics Card
512Mb NVIDIA GeForce 8600M GT
Hard Drive
200Gb 7200RPM
OS
OSX 10.5.2 / XP Pro / Boot Camp + Parallels
Monitor
17" Matte 1920x1200
Scriptorum is offline Overclocked Account   Reply With Quote
Old 06-30-08   #14 (permalink)
Commodore 64
 
nategr8ns's Avatar
 
amd nvidia

Join Date: Dec 2007
Location: Maine
Posts: 4,278

Rep: 130 nategr8ns is acknowledged by manynategr8ns is acknowledged by many
Unique Rep: 109
Folding Team Rank: 241
Trader Rating: 3
Default

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?

System: OptyMSI Prime (soon "DFO: Designed for Optyvation"
CPU
Opteron 175 LCB9E 0652WPMW
Motherboard
MSI K8N Neo4-f (DFI NF4 Ultra-D en-closet)
Memory
2x1GB GSkill 500mhz HZs
Graphics Card
EVGA 8800GT SC 512
Hard Drive
160GB + 120GB
Sound Card
onboard ftw!
Power Supply
Ultra X-Finity 600w
Case
AeroCool AeroEngine II <3
CPU cooling
ACFreezer Pro w/92mm+80mm+120mm
GPU cooling
AC Accelero S1 w/Dual Tri-Cool 120mm (low)
OS
XP Home + Vista Ultimate after Mobo upgrade
Monitor
Samsung 220WM 22" + LG f-Engine 17" (dual screen)
nategr8ns is offline I fold for Overclock.net   Reply With Quote
Old 06-30-08   #15 (permalink)
Chiefly Ignorant
 
Scriptorum's Avatar
 
intel nvidia

Join Date: Jan 2008
Location: Atlanta, GA
Posts: 54
Blog Entries: 12

Rep: 13 Scriptorum Unknown
Unique Rep: 10
Trader Rating: 0
Default

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.
__________________
Quote:
Originally Posted by The Bartender Paradox View Post
crazy?...nah. when you cool with a fire extinguisher then your crazy.

System: Flaming Moe's MacBook Pro
CPU
Core 2 Duo
Motherboard
MacBook Pro
Memory
4GB DDR2-667
Graphics Card
512Mb NVIDIA GeForce 8600M GT
Hard Drive
200Gb 7200RPM
OS
OSX 10.5.2 / XP Pro / Boot Camp + Parallels
Monitor
17" Matte 1920x1200
Scriptorum is offline Overclocked Account   Reply With Quote
Old 07-01-08   #16 (permalink)
Commodore 64
 
nategr8ns's Avatar
 
amd nvidia

Join Date: Dec 2007
Location: Maine
Posts: 4,278

Rep: 130 nategr8ns is acknowledged by manynategr8ns is acknowledged by many
Unique Rep: 109
Folding Team Rank: 241
Trader Rating: 3
Default

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

System: OptyMSI Prime (soon "DFO: Designed for Optyvation"
CPU
Opteron 175 LCB9E 0652WPMW
Motherboard
MSI K8N Neo4-f (DFI NF4 Ultra-D en-closet)
Memory
2x1GB GSkill 500mhz HZs
Graphics Card
EVGA 8800GT SC 512
Hard Drive
160GB + 120GB
Sound Card
onboard ftw!
Power Supply
Ultra X-Finity 600w
Case
AeroCool AeroEngine II <3
CPU cooling
ACFreezer Pro w/92mm+80mm+120mm
GPU cooling
AC Accelero S1 w/Dual Tri-Cool 120mm (low)
OS
XP Home + Vista Ultimate after Mobo upgrade
Monitor
Samsung 220WM 22" + LG f-Engine 17" (dual screen)
nategr8ns is offline I fold for Overclock.net   Reply With Quote
Old 07-01-08   #17 (permalink)
Chiefly Ignorant
 
Scriptorum's Avatar
 
intel nvidia

Join Date: Jan 2008
Location: Atlanta, GA
Posts: 54
Blog Entries: 12

Rep: 13 Scriptorum Unknown
Unique Rep: 10
Trader Rating: 0
Default

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.
__________________
Quote:
Originally Posted by The Bartender Paradox View Post
crazy?...nah. when you cool with a fire extinguisher then your crazy.

System: Flaming Moe's MacBook Pro
CPU
Core 2 Duo
Motherboard
MacBook Pro
Memory
4GB DDR2-667
Graphics Card
512Mb NVIDIA GeForce 8600M GT
Hard Drive
200Gb 7200RPM
OS
OSX 10.5.2 / XP Pro / Boot Camp + Parallels
Monitor
17" Matte 1920x1200
Scriptorum is offline Overclocked Account   Reply With Quote
Old 07-01-08   #18 (permalink)
Intel Overclocker
 
DiagnosisDirt's Avatar
 
intel nvidia

Join Date: Nov 2005
Location: Florida
Posts: 1,336
Blog Entries: 2

Rep: 126 DiagnosisDirt is acknowledged by manyDiagnosisDirt is acknowledged by many
Unique Rep: 103
FAQs Submitted: 1
Folding Team Rank: 765
Team Name: RatRod
Trader Rating: 1
Default

Always check the webmonkey. (beginner flash tutorial)
__________________
Go Forth, to linux you must.
RatRod!




System: Craiginator
CPU
e6420 - L712a517
Motherboard
Abit AB9 Pro P965
Memory
2GB GEIL PC2-8500 Ultra
Graphics Card
XFX 7800 GTX
Hard Drive
2x Wd 160gb Raid 0
Sound Card
SoundMax Hd
Power Supply
Antec 550 True Power
Case
Antec 900
CPU cooling
Tuniq Tower
OS
Ubuntu / XPlite
Monitor
22' Acer ws X222W
DiagnosisDirt is offline I fold for Overclock.net DiagnosisDirt's Gallery   Reply With Quote
Old 07-01-08   #19 (permalink)
Commodore 64
 
nategr8ns's Avatar
 
amd nvidia

Join Date: Dec 2007
Location: Maine
Posts: 4,278

Rep: 130 nategr8ns is acknowledged by manynategr8ns is acknowledged by many
Unique Rep: 109
Folding Team Rank: 241
Trader Rating: 3
Default

double , I meant upper left
thanks diagnosis, I'll check that out

System: OptyMSI Prime (soon "DFO: Designed for Optyvation"
CPU
Opteron 175 LCB9E 0652WPMW
Motherboard
MSI K8N Neo4-f (DFI NF4 Ultra-D en-closet)
Memory
2x1GB GSkill 500mhz HZs
Graphics Card
EVGA 8800GT SC 512
Hard Drive
160GB + 120GB
Sound Card
onboard ftw!
Power Supply
Ultra X-Finity 600w
Case
AeroCool AeroEngine II <3
CPU cooling
ACFreezer Pro w/92mm+80mm+120mm
GPU cooling
AC Accelero S1 w/Dual Tri-Cool 120mm (low)
OS
XP Home + Vista Ultimate after Mobo upgrade
Monitor
Samsung 220WM 22" + LG f-Engine 17" (dual screen)
nategr8ns is offline I fold for Overclock.net   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 10:04 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.23248 seconds with 10 queries