Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411613 Posts in 69390 Topics- by 58447 Members - Latest Member: sinsofsven

May 10, 2024, 03:01:59 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Learning to program in Flash (title changed for obvius (or not) reason)
Pages: [1] 2
Print
Author Topic: Learning to program in Flash (title changed for obvius (or not) reason)  (Read 3745 times)
davidp
Level 6
*



View Profile WWW
« on: November 02, 2009, 01:52:46 PM »

Now I don't know if this is the right place or not, but I'll post her anyway (and hope I've landed on a right place after all).

Soooooo after many, many years of doing nothing (well actually i did some things), I've become mega productive (it seems that ending school and not being able to find a decent job awaken productivity, during boring days, especially now in almost-winter time of the year. but enough with this offtopic...) and started working (with) and learning python, c++, and...

Flash.

and now, i've been following this tutorial @ prototyprally and i've came to this loop thingy and keyboard input events.

i'm using this:

Code:
public function Main():void
{
  addEventListener(Event.ENTER_FRAME, handleEnterFrame);
  ...
}

then this:

private function handleEnterFrame(e:Event):void
Code:
{
  stage.addEventListener(KeyboardEvent.KEY_DOWN, checkPressed);
  addEventListener(Event.ENTER_FRAME, handleEnterFrame);
}
   

and finally this:

Code:
private function checkPressed(e:KeyboardEvent):void
{
  if (e.keyCode == 37)  //left
  player.x -= 4;
}

tl:dr version:

main ENTER_FRAME, function keypressed, function ENTER_FRAME, do movement, repeat.

But, this is rather strange... movement is kinda choppy, can't really explain, but let's just say, not smooth. if i add

enemy.x += 4;

in the ENTER_FRAME handler for instance, that enemy moves really slick and without jerking (lol.)

so i'm wondering if this is the best way, or could i optimize somewhere or what do you suggest?

« Last Edit: November 03, 2009, 03:04:40 AM by davidp » Logged

Glaiel-Gamer
Guest
« Reply #1 on: November 02, 2009, 01:59:21 PM »

don't put logic in the keydown event or in the keyup event, cache the values.

Keydown registers kinda like when you're typing and hold down a key, it registers once, waits a couple seconds, then registers at about 5 times / seconds.

Just toggle a boolean in the keydown and keyup events and handle that in the enterframe event
Logged
Draknek
Level 6
*


"Alan Hazelden" for short


View Profile WWW
« Reply #2 on: November 02, 2009, 02:14:58 PM »

Glaiel-Gamer is right, you need a variable telling you whether a key is up or down, and use the KEY_UP/KEY_DOWN events to set that variable.

Then in handleEnterFrame you put the player movement:

Code:
if (left)  { player.x -= 4; }
if (right) { player.x += 4; }

And another, less important thing:

Code:
private function handleEnterFrame(e:Event):void 
{
  stage.addEventListener(KeyboardEvent.KEY_DOWN, checkPressed);
  addEventListener(Event.ENTER_FRAME, handleEnterFrame);
}

What's this doing here? You only need to add an EventListener once: here you're adding them every frame.

It won't actually do anything bad; it's just ignored. But it makes more sense to put that stuff in Main (like handleEnterFrame already is: you don't need it twice).
Logged

desdinova
Level 0
***



View Profile
« Reply #3 on: November 02, 2009, 02:26:19 PM »

If you need more help, this tutorial might help explain how to do keyboard events http://www.newgrounds.com/bbs/topic/773868
Logged
YagerX
Level 2
**



View Profile WWW
« Reply #4 on: November 02, 2009, 02:28:08 PM »

What they be sayin.

If you're feeling confident, then you could use an array to store the values too!
ie. keyIn[0] could be left
keyIn[1] could be right and so on.
thing is that you'd have to remember which value in the array is which key.
One way that I find handy is to make an array where the ids are equivalent to the keyIDs
so keyIn[37] would contain you left key's values. But if you're only going to be using 3-4 keys there's not much point.

I originally just typed the first line but then felt my post was a bit redundant  Embarrassed
Logged

davidp
Level 6
*



View Profile WWW
« Reply #5 on: November 02, 2009, 03:06:52 PM »

@desdinova:

thanks this really helped Smiley

as mentioned in tutorial i created 2d array with all the used buttons and state false/true and then checked in handleEnterFrame function if any key is pressed and performed corresponding action.

really tutorials on newgrounds.
« Last Edit: November 02, 2009, 05:02:23 PM by davidp » Logged

Lifesnoozer
Level 2
**


wait... who?


View Profile WWW
« Reply #6 on: November 02, 2009, 03:28:26 PM »

Learning to flash!?  In public!? My Word!

I had to.
Logged

davidp
Level 6
*



View Profile WWW
« Reply #7 on: November 02, 2009, 03:33:22 PM »

well yea, it don't really know any place where i could go and ask a question and hope for an answer, besides tigs. it would be kinda... stupid just to register on some unknown and cold place.


tigsource Kiss

 Ninja
Logged

Hideous
That's cool.
Level 10
*****


3D models are the best


View Profile WWW
« Reply #8 on: November 02, 2009, 03:44:46 PM »

woosh
Logged

Lifesnoozer
Level 2
**


wait... who?


View Profile WWW
« Reply #9 on: November 02, 2009, 03:52:01 PM »

I'm at a total loss of words. Good day.
Logged

davidp
Level 6
*



View Profile WWW
« Reply #10 on: November 02, 2009, 05:08:40 PM »

it's ok, because i asked and learned something new - and atm everything works smooth. so why not. better to ask than wonder in dark hopelessly.

 Lips Sealed
Logged

John Nesky
Level 10
*****


aka shaktool


View Profile WWW
« Reply #11 on: November 02, 2009, 05:17:12 PM »

To dispel misunderstanding, I am now going to explain a joke. I apologize to anyone who did not need it explained.

Learning to flash!?  In public!? My Word!

This is a reference to exhibitionism, also known as "flashing". It was not intended to be insulting.
Logged
davidp
Level 6
*



View Profile WWW
« Reply #12 on: November 03, 2009, 03:00:40 AM »

yea, i know. i was thinking about it when i woke up today.
now when i think of it.. it really is funny :D

 Facepalm my bad, even if we all know what topic is about Tongue
in my defense: it was really late when everything was happening.
« Last Edit: November 03, 2009, 03:11:04 AM by davidp » Logged

davidp
Level 6
*



View Profile WWW
« Reply #13 on: November 04, 2009, 05:28:01 AM »

edit: i've been porting my project from flashdevelop and found out that none of the pressed letters (a,b,c,d,e,f,g,etc...) gets recognized by KEY_DOWN (or KEY_UP) event in adobe flash 4. so what should i do?

Code:
public function keyDownEvent(e:KeyboardEvent):void
{
  char_table[e.keyCode][1]=true;
  trace(e.keyCode);
}
pressing S in adobe flash - nothing happens
pressing S in flash develop - 83 in output window

any suggestions?

edit 2: nevermind, found the solution

Quote
It should be noted that when testing a movie in the IDE, Flash will intercept all key presses that are mapped to Flash actions. So, if I intend to trap W, A, S and D, but I test the movie in Flash, A and S will not make it through to the movie, but intercepted by Flash. Which means that in order to properly test the movie you have to publish it and test externally. Very annoying.
« Last Edit: November 04, 2009, 05:47:25 AM by davidp » Logged

dustin
Level 6
*


View Profile
« Reply #14 on: November 04, 2009, 07:14:49 AM »

I have found a way around this that works for me.  Press shift S and the ide won't catch it but your game will.
Logged
davidp
Level 6
*



View Profile WWW
« Reply #15 on: November 04, 2009, 08:24:04 AM »

y, i noticed. mah, i've just put shooting on shift for testing purposes.
Logged

Drew
Level 0
***


Hooray


View Profile
« Reply #16 on: November 04, 2009, 09:35:55 AM »

There's a menu option in the flash player (when run from the IDE) called "disable keyboard shortcuts."
Logged
davidp
Level 6
*



View Profile WWW
« Reply #17 on: November 07, 2009, 06:26:20 AM »

thanks for the hint drew.


hm, now i have basic movement and player control engine down and i'm wondering how to work with "view" (if such thing even exists in ac3). what i want to do is like have movieclip of a let's say, 800x800 and want a view of 400x400 follow my character. could anyone point me in some direction?

i was googling for views in ac3 but found nothing of a relevance.
Logged

YagerX
Level 2
**



View Profile WWW
« Reply #18 on: November 08, 2009, 01:44:24 PM »

Are you saying that you want to zoom/scale everything up or do you want to only use a section of the screen?
Logged

davidp
Level 6
*



View Profile WWW
« Reply #19 on: November 08, 2009, 01:54:19 PM »

no, i want would like to follow player's character where he goes. like so:

so player moves around movie clip and view follows him. its like i have 800x800 movie clip, but the flash screen is only 400x400 and follows player around when moving on mc.
« Last Edit: November 08, 2009, 01:57:58 PM by davidp » Logged

Pages: [1] 2
Print
Jump to:  

Theme orange-lt created by panic