Would you like to react to this message? Create an account in a few clicks or log in to continue.

4 posters

    AS3 coder here?

    avatar
    [KHK]Antumarin
    Real Spammer
    Real Spammer


    Posts : 417
    Join date : 2013-02-01
    Age : 28

    AS3 coder here? Empty AS3 coder here?

    Post by [KHK]Antumarin Fri Jun 21, 2013 6:20 pm

    I have as homework to finish a game and it's due Monday. The professor taught us hitTestObject but that's it and I need to make turn a move clip into a wall. Every other classmate is making a ball that on contact with a "wall" bounces but I'm trying to make a character that once it hits the wall it stops walking into it. I archived something like that but only with one axis (either x, -x, y or -y) and the wall is meant to be solid from every axis.
    One more thing, the movie clip I'm using has transparent sections (it was made from a png file) so I need the character not to collide with these sections.
    If you need a better explanation I'll try but please I've been looking everywhere in the internet and it only shows ball bouncing on the walls, I asked my classmates but nobody knows and I sent mails to my professor but he doesn't answer.

    Here are the objects in which the character is supposed to collide:

    AS3 coder here? Objetos
    [KHK]Khalid
    [KHK]Khalid
    Admin


    Posts : 464
    Join date : 2011-05-01

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Khalid Sat Jun 22, 2013 3:26 am

    I really don't know much about ActionScript. You're saying that you could detect when a character hits an object from only ONE (I don't understand this: "either x, -x, y or -y") axis but not all? Can you show code?


    Oh and have you tried Stack Overflow (http://stackoverflow.com/)? It really helps.
    avatar
    [KHK]Antumarin
    Real Spammer
    Real Spammer


    Posts : 417
    Join date : 2013-02-01
    Age : 28

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Antumarin Sat Jun 22, 2013 4:52 am

    No, I meant that hitTestObject detects when a object overlaps another but when I try to make them collide, that's when it only works from one side.
    Code:
    if (personaje.hitTestObject(muro)
    {
       personaje.x -= 5;
    }
    Being "personaje" the character, "muro" the wall in which it's meant to collide with. I gave that idea up since I'm sure there is no way it could work and instead I tried this:
    Code:
    if (personaje.hitTestObject(muro)
    {
       velocidadPersonaje -= 5;
    }
    Being "velocidadPersonaje" the speed the character moves with. It may work if it weren't for the fact that "muro" is a png turned into a movie clip (for instance the character is inside it and cannot move). I heard there is a way but I can't seem to make it work. Something like these:
    http://gskinner.com/blog/archives/2005/10/source_code_sha.html
    http://www.freeactionscript.com/2011/08/as3-pixel-perfect-collision-detection/

    And btw, yes I tried Stack Overflow already, the same thing happened, not what I'm looking for.
    [KHK]Khalid
    [KHK]Khalid
    Admin


    Posts : 464
    Join date : 2011-05-01

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Khalid Sat Jun 22, 2013 5:39 am

    Hmm this might work (and fix the only one axis problem) if I clearly understood what you said.

    Make 2 more variables for personaje

    Code:

    personaje.lastFreeX
    personaje.lastFreeY

    We will run a repeated timer (or maybe use the main process thread where you're checking if hitTestObject and these stuff) (With interval 500 ms maybe? I dunno this needs some experiments to make it work properly) and we'll be storing personaje's X (AS LONG AS hitObjectTest returns FALSE (i.e it doesn't overlap an object)) in its personaje's lastFreeX, same goes to Y and lastFreeY. And then we can use LastFreeX/Y to return personaje when it overlaps muro to its last position where it was right before overlapping . Here's an example of what I said, there may be some syntax errors:

    Code:

    theTimer() // or main thread
    {
        if(!personaje.hitTestObject(muro)) // Using "!". If personaje isn't overlapping muro.
        {
            personaje.lastFreeX = personaje.x;
            personaje.lastFreeY = personaje.y;
        }
        else // Else. If it's overlapping muro .. same as if(personaje.hitTestObject(muro))
        {
            // reset its position to the last FREE position we stored.
            personaje.x = personaje.lastFreeX;
            personaje.y = personaje.lastFreeY;
        }
    }


    If this doesn't work, come back and show me the code you've wrote for it and we shall workout it. I think the only thing that can make it fail is the interval of that timer as it doesn't have to check/store the x/y and lastFreeX/Y MANY times nor FEW times.
    avatar
    [KHK]Antumarin
    Real Spammer
    Real Spammer


    Posts : 417
    Join date : 2013-02-01
    Age : 28

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Antumarin Sat Jun 22, 2013 6:08 am

    That's a very nice idea, but sadly I can't really put it to work since (as the picture in the original post shows) every wall is connected in a single file. If I can make this Pixel Perfect Collision thing and use your lastFreeX/Y idea then it would end up just perfect. I just need to make that work.
    [KHK]Khalid
    [KHK]Khalid
    Admin


    Posts : 464
    Join date : 2011-05-01

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Khalid Sat Jun 22, 2013 6:13 am

    What do you mean by every wall is connected in a single file?
    avatar
    [KHK]Antumarin
    Real Spammer
    Real Spammer


    Posts : 417
    Join date : 2013-02-01
    Age : 28

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Antumarin Sat Jun 22, 2013 6:18 am

    Look at the picture at the original post. That's what the character must collide into.
    [KHK]Khalid
    [KHK]Khalid
    Admin


    Posts : 464
    Join date : 2011-05-01

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Khalid Sat Jun 22, 2013 6:21 am

    I've already looked. Maybe I don't understand you? Are you sure that code won't work?
    avatar
    [KHK]Antumarin
    Real Spammer
    Real Spammer


    Posts : 417
    Join date : 2013-02-01
    Age : 28

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Antumarin Sat Jun 22, 2013 6:24 am

    I already tried. Technically the character is always in contact with the walls, that's what I gotta solve.
    [KHK]Khalid
    [KHK]Khalid
    Admin


    Posts : 464
    Join date : 2011-05-01

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Khalid Sat Jun 22, 2013 6:33 am

    Okay it's always in contact with the walls, what's the problem? Don't you have hitTestObject function which detects when it overlaps any of those objects? Didn't this code by you work

    Code:

    if (personaje.hitTestObject(muro)
    {
       personaje.x -= 5;
    }

    ?

    I hope you can show all the code here (including what you wrote for my idea), I don't think anyone here will steal it or something like this.
    avatar
    [KHK]Antumarin
    Real Spammer
    Real Spammer


    Posts : 417
    Join date : 2013-02-01
    Age : 28

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Antumarin Sat Jun 22, 2013 6:40 am

    I have no problem, here is what I have:

    Code:
    package
    {
       import flash.display.Sprite;
       import flash.events.Event;
       import flash.events.KeyboardEvent;
       import flash.text.TextField;
       import flash.text.TextFormat;
       import flash.text.TextFormatAlign;
       import flash.ui.Keyboard;
       
       [SWF(width="800",height="600",frameRate="36",backgroundColor="0x000000")]
       public class Main extends Sprite
       {
          public var salida:Sprite = new exit;
          public var enemigo1:Sprite;
          public var enemigo2:Sprite;
          public var enemigo3:Sprite;
          public var vistaEnemigo1:Sprite;
          public var vistaEnemigo2:Sprite;
          public var vistaEnemigo3:Sprite;
          public var personaje:Sprite;
          public var floor:Sprite = new piso;
          public var walls:Sprite = new objetos;
          public var maletin:Sprite = new maletin1;
          
          public var puntaje:TextField = new TextField();
          public var formato:TextFormat = new TextFormat();
          public var puntos:int = 0;
          
          public var direccionEnemigo1:int =1;
          public var direccionEnemigo2:int =1;
          public var direccionEnemigo3:int =1;
          public var velocidadEnemigo1:int =5;
          public var velocidadEnemigo2:int =5;
          public var velocidadEnemigo3:int =5;
          public var velocidadPersonaje:int =5;
          
          public var personajeLastFreeX:int;
          public var personajeLastFreeY:int;
          
          public var derecha:Boolean = false;
          public var izquierda:Boolean = false;
          public var arriba:Boolean = false;
          public var abajo:Boolean = false;
          public var inicio:Boolean = true;
          public var escape:Boolean = false;
          
          
          public function Main()
          {
             enemigo1 = drawCircle (0x263ed0,20,1);
             enemigo2 = drawCircle (0x263ed0,20,1);
             enemigo3 = drawCircle (0x263ed0,20,1);
             vistaEnemigo1 = drawCircle (0xb6b400,100,0.5);
             vistaEnemigo2 = drawCircle (0xb6b400,100,0.5);
             vistaEnemigo3 = drawCircle (0xb6b400,100,0.5);
             personaje = drawCircle (0xff0000,20,1);
             
             stage.addChild(floor);
             stage.addChild(vistaEnemigo1);
             stage.addChild(vistaEnemigo2);
             stage.addChild(walls);
             //stage.addChild(vistaEnemigo3);
             stage.addChild(salida);
             stage.addChild(enemigo1);
             stage.addChild(enemigo2);
             //stage.addChild(enemigo3);
             stage.addChild(personaje);
             stage.addChild(maletin);
             
             floor.x = 0;
             floor.y = 0;
             
             walls.x = 0;
             walls.y = 0;
             
             salida.x = 759;
             salida.y = 500;
             
             maletin.x = 146;
             maletin.y = 100;
             /*var azarX:Number;
             var azarY:Number;
             azarX = random (38,146);
             azarY = random (74,100);
             maletin.x = azarX;
             maletin.y = azarY;
             */
             enemigo1.x = 740;
             enemigo1.y = 110;
             
             enemigo2.x = 740;
             enemigo2.y = 247;
             
             /*enemigo3.x = 749;
             enemigo3.y = 547;
             enemigo3.visible = false;
             vistaEnemigo3.visible = false;
             */
             personaje.x = 749;
             personaje.y = 547;
             
             puntaje.text = "Puntos :"+puntos;
             formato.font = "Verdana";
             formato.size = 20;
             formato.align = TextFormatAlign.CENTER;
             puntaje.setTextFormat(formato);
             puntaje.x = 109;
             puntaje.y = 566;
             addChild (puntaje);
             
             stage.addEventListener(Event.ENTER_FRAME,update);
             stage.addEventListener(KeyboardEvent.KEY_UP,apreto);
             stage.addEventListener(KeyboardEvent.KEY_DOWN,solto);
          }
          
          public function random(min:int,max:int):int
          {
             var num:int = (Math.random() * (max - min)) + min;
             return num;
          }
          
          public function apreto(evento:KeyboardEvent):void
          {
             if (evento.keyCode == Keyboard.W)
             {
                arriba = true;
             }
             
             if (evento.keyCode == Keyboard.S)
             {
                abajo = true;
             }
             
             if (evento.keyCode == Keyboard.A)
             {
                izquierda = true;
             }
             
             if (evento.keyCode == Keyboard.D)
             {
                derecha = true;
             }
          }
          
          public function solto(evento:KeyboardEvent):void
          {
             if (evento.keyCode == Keyboard.W)
             {
                arriba = false;
             }
             
             if (evento.keyCode == Keyboard.S)
             {
                abajo = false;
             }
             
             if (evento.keyCode == Keyboard.A)
             {
                izquierda = false;
             }
             
             if (evento.keyCode == Keyboard.D)
             {
                derecha = false;
             }
          }
          
          public function update(evento:Event):void
          {
             vistaEnemigo1.y = enemigo1.y + enemigo1.height;
             vistaEnemigo1.x = enemigo1.x + enemigo1.width;
             
             vistaEnemigo2.y = enemigo2.y + enemigo1.height;
             vistaEnemigo2.x = enemigo2.x + enemigo1.width;
             
             vistaEnemigo3.y = enemigo3.y + enemigo1.height;
             vistaEnemigo3.x = enemigo3.x + enemigo1.width;
                      
             if (personaje.hitTestObject(maletin))
             {
                inicio = false;
                escape = true;
             }
             
             if (personaje.hitTestObject(salida))
             {
                puntos += 1;
                inicio = true;
                escape = false;
             }
             
             if (inicio == true)
             {
                maletin.visible = false;
                salida.visible = true;
                if (personaje.hitTestObject(enemigo1) || personaje.hitTestObject(enemigo2) || personaje.hitTestObject(enemigo3))
                {
                   personaje.x = 749;
                   personaje.y = 547;
                }
             }
             
             if (escape == true)
             {
                maletin.visible = false;
                salida.visible = true;
                if (personaje.hitTestObject(enemigo1) || personaje.hitTestObject(enemigo2) || personaje.hitTestObject(enemigo3))
                {
                   personaje.x = 217;
                   personaje.y = 105;
                }
             }
             
             if (personaje.x == 0 - personaje.width/4)
             {
                personaje.x += 5;
             }
             
             if (personaje.x == stage.stageWidth - personaje.width/4)
             {
                personaje.x -= 5;
             }
             
             if (personaje.y == 0 - personaje.height/4)
             {
                personaje.y += 5;
             }
             
             if (personaje.y == stage.stageHeight - personaje.height/4)
             {
                personaje.y -= 5;
             }
             
             enemigo2.x += velocidadEnemigo2 * direccionEnemigo2;
             if (enemigo2.x >= stage.stageWidth - 100)
             {
                direccionEnemigo2 = -1;
             }
             
             if (enemigo2.x <= 100)
             {
                direccionEnemigo2 = 1;
             }
             
             enemigo3.x += velocidadEnemigo3 * direccionEnemigo3;
             if (enemigo3.x >= stage.stageWidth - 100)
             {
                direccionEnemigo3 = -1;
             }
             
             if (enemigo2.x <= 100)
             {
                direccionEnemigo3 = 1;
             }
             
             if (izquierda == true)
             {
                personaje.x -= velocidadPersonaje;
             }
             if (derecha == true)
             {
                personaje.x += velocidadPersonaje;
             }
             if (arriba == true)
             {
                personaje.y -= velocidadPersonaje;
             }
             if (abajo == true)
             {
                personaje.y += velocidadPersonaje;
             }

             if(!personaje.hitTestObject(walls))
             {
             personajeLastFreeX = personaje.x;
             personajeLastFreeY = personaje.y;
             }
             else
             {
             personaje.x = personajeLastFreeX;
             personaje.y = personajeLastFreeY;
             }
          }
          
          public function drawCircle(color:int,radio:int,alpha:Number):Sprite
          {
             var dibujo:Sprite = new Sprite;
             dibujo.graphics.beginFill(color,alpha);
             dibujo.graphics.drawCircle(-radio/2,-radio/2,radio);
             dibujo.graphics.endFill();
             return dibujo;
          }
       }
    }

    And by the way, the thing is that even the transparent sections are taken as another part of "walls" (the new name for "muro", had to rename it because of reasons).
    [KHK]Khalid
    [KHK]Khalid
    Admin


    Posts : 464
    Join date : 2011-05-01

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Khalid Sat Jun 22, 2013 6:47 am

    What else is part of the "walls"?

    I'm checking the code right now.
    avatar
    [KHK]Antumarin
    Real Spammer
    Real Spammer


    Posts : 417
    Join date : 2013-02-01
    Age : 28

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Antumarin Sat Jun 22, 2013 6:59 am

    Look, the program takes the "walls" as a stage-sized rectangle. The whole thing is the "wall". I've read there's a way to add a bitmap to the movie clip and that may be the solution. If I'm lucky and tomorrow I have my class of Coding for Facebook Applications then I may ask the professor about it.
    I could grab "walls" and cut it in different sections in Photoshop. It would make it work but that would make everything harder, that'd be my last choice.
    I don't know what time is there but it's almost 4 am here and the latter I'm going to sleep today the harder will be waking up tomorrow at morning. Thanks for all the help, man. This work is really important for my notes and I appreciate you giving me a hand here.
    [KHK]Khalid
    [KHK]Khalid
    Admin


    Posts : 464
    Join date : 2011-05-01

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Khalid Sat Jun 22, 2013 7:03 am

    I got some questions but let's see if you're gonna fix it tomorrow. And you're welcome.
    Aiman
    Aiman
    Spamming Machine
    Spamming Machine


    Posts : 543
    Join date : 2013-02-01
    Age : 24
    Location : India

    AS3 coder here? Empty Re: AS3 coder here?

    Post by Aiman Sat Jun 22, 2013 4:37 pm

    Coders ;D
    [KHK]BlueBaron
    [KHK]BlueBaron
    Real Spammer
    Real Spammer


    Posts : 230
    Join date : 2013-02-01
    Age : 26
    Location : Skopje,Macedonia

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]BlueBaron Sat Jun 22, 2013 4:46 pm

    Well, i don't understand anything in here. But, look at this: http://www.powercursor.com/

    Maybe check the code how its done, and use it for that little man. Its for Flash, you using Flash?
    avatar
    [KHK]Antumarin
    Real Spammer
    Real Spammer


    Posts : 417
    Join date : 2013-02-01
    Age : 28

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Antumarin Sat Jun 22, 2013 6:04 pm

    Back. Bad luck, college was closed today. Until anything new appears I'll try to make it work cutting the "walls" with Photoshop and adding every section separated.

    [KHK]BlueBaron wrote:Well, i don't understand anything in here. But, look at this: http://www.powercursor.com/

    Maybe check the code how its done, and use it for that little man. Its for Flash, you using Flash?
    Flash Builder, actually. That page doesn't really help but it looks very interesting. I'll take a look at Power Cursor once I finish this work.
    [KHK]Khalid
    [KHK]Khalid
    Admin


    Posts : 464
    Join date : 2011-05-01

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Khalid Sat Jun 22, 2013 7:40 pm

    Can I see the code of hitTestObject? What's the use of it then if everything is "walls"? Won't the character be hitting walls all the time?

    Edit:

    Yeah I think turning it into a bitmap (not in a single file though) will do the trick. I once created a 2D game which was based on the same idea as your script did the same thing but with bitmap images and it worked fine - I downloaded them though Razz.
    avatar
    [KHK]Antumarin
    Real Spammer
    Real Spammer


    Posts : 417
    Join date : 2013-02-01
    Age : 28

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Antumarin Sat Jun 22, 2013 9:08 pm

    Fuck it.
    I wanted to add all these details to make it look prettier. To be honest he said we must use the shapes made with Flash Builder (you know, squares and circles ¬_¬) but if we felt like a challenge we could also try to make a nicely looking game with Photoshop-made stuff like I was trying to do, but with the little he taught us ( horray for college education :/ ) I really can't do crap.
    I'll start the code from zero and using Flash Builder shapes, maybe I can add other crap but that's if I feel like after all this madness.
    I'll make the current game a separated work, more personal so the next time I see him I can ask him shit until mid-fucking-night. The professor is a great guy and all but if he can't teach us basic coding in four months he's doing his job wrong. We haven't even talked about classes, ffs.
    [KHK]Khalid
    [KHK]Khalid
    Admin


    Posts : 464
    Join date : 2011-05-01

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Khalid Sat Jun 22, 2013 10:23 pm

    Yeah, college education sucks at these things. Fortunately, I've never been taught anything about coding by a teacher - books and net were my only friends. Anyway, good luck and remember to show it to us when you're done Smile.
    avatar
    [KHK]Antumarin
    Real Spammer
    Real Spammer


    Posts : 417
    Join date : 2013-02-01
    Age : 28

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Antumarin Sat Jun 22, 2013 10:30 pm

    Of course I will. I need something to be proud of, heh. Have you learn by looking around the internet for answers or by watching tutorials at a page in particular?
    [KHK]Khalid
    [KHK]Khalid
    Admin


    Posts : 464
    Join date : 2011-05-01

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Khalid Sat Jun 22, 2013 11:34 pm

    Looking at other people's (not any) code and see how they got things done helped me get started. Plus, I started with editing some simple scripts I found on net to make them work as I wanted. My method was like this: read stuff, write stuff, fail, try to fix, (if) fail to fix, (then) read and research and so on (in short: trial and error). And finally I started to write my own scripts.
    [KHK]BlueBaron
    [KHK]BlueBaron
    Real Spammer
    Real Spammer


    Posts : 230
    Join date : 2013-02-01
    Age : 26
    Location : Skopje,Macedonia

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]BlueBaron Sun Jun 23, 2013 10:06 am

    AS3 coder here? Fuckthat

    See other peoples work > Figure out the code > Fail! > Watch TV/Play video games. <- Repeat.

    Result:  Fuck programming, its too boring.
    avatar
    [KHK]Antumarin
    Real Spammer
    Real Spammer


    Posts : 417
    Join date : 2013-02-01
    Age : 28

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]Antumarin Sun Jun 23, 2013 6:33 pm

    Some people find it boring but it's interesting how you can make things move with a few lines of code (if you know what you're doing even better, not my case). With making textures for 3D they are both the things I enjoy doing the most... Right after being lazy.
    [KHK]BlueBaron
    [KHK]BlueBaron
    Real Spammer
    Real Spammer


    Posts : 230
    Join date : 2013-02-01
    Age : 26
    Location : Skopje,Macedonia

    AS3 coder here? Empty Re: AS3 coder here?

    Post by [KHK]BlueBaron Sun Jun 23, 2013 7:02 pm

    Yea, i can pawn alright. But I iz lazy.

    Sponsored content


    AS3 coder here? Empty Re: AS3 coder here?

    Post by Sponsored content


      Current date/time is Thu Mar 28, 2024 5:16 pm