hittest y posición

  • ryrocks
  • Newbie
  • Newbie
  • No Avatar
  • Registrado: Feb 13, 2009
  • Mensajes: 6
  • Status: Offline

Nota Febrero 16th, 2009, 1:36 pm

¡Eh!,
Tengo 2 classs movieclip.
-Una bola de control que
-una serie de rectángulos que el balón "tierras" en.

utilizando un HitTest, cuando la bola "hits" el movieclip rectángulo que se detenga directamente en la parte superior del rectángulo.

Tengo 3 rectángulos todos vinculados a la misma clase que contiene el HitTest.

Mi problema es que cuando muevo la bola a través de cada rectángulo, no de la tierra en el mismo lugar cada vez. Creo que esto es porque desde que soy utilizando un evento ENTER_FRAME, el hitCheck no coincide con las bolas de primer contacto real y, a veces hace que la pelota se mueven un poco más de lo deseado.

Ive subido el archivo. Swf: http://www.activ8-3d.co.uk/ryan/test/floor.swf
(utilice las teclas de flecha para controlar)

Para superar esto añadido a la HitTest:
[como]
if (_ball_mc.hitTestObject (this))
(
_ball_mc.onGround = true;
)
....
if (_ball_mc.onGround)
(
_ball_mc.y = esto. y - this.height - 13;
)
[/ as]

= _ball_mc.y this.y - this.height - 13; es la posición correcta, quiero que la bola a la tierra cada vez, pensé que tendría la rompió, pero, ¡ay! Su tal un problema menor que no quería pedir ayuda en él, pero yo no aguanto más!

Heres el código completo:

Clase de documento Ball.as controla el balón:

Código: [ Select ]
package
{
    import flash.ui.Keyboard;
    import flash.events.*;
    import flash.display.*;

    public class Ball extends MovieClip
    {     
        public var leftDown:Boolean = false;
        public var rightDown:Boolean = false;
        public var jump:Boolean = false;
        public var velocity:Number = 20;
        public var falling:Boolean = true;
        public var speed:int = 5;
        public var onGround:Boolean = false;
    
        public function Ball()
        {         
            stage.addEventListener(KeyboardEvent.KEY_DOWN, downDetect);
            stage.addEventListener(KeyboardEvent.KEY_UP, upDetect);
            stage.addEventListener(Event.ENTER_FRAME, moveChar);
        }
        
        public function downDetect(event:KeyboardEvent):void
        {
            if(event.keyCode == Keyboard.LEFT)
            {
                leftDown = true;
            }
            if(event.keyCode == Keyboard.RIGHT)
            {
                rightDown = true;
            }
            if(event.keyCode == Keyboard.UP && !falling)
            {
                jump = true;
            }
        }
        
        
        public function upDetect(event:KeyboardEvent):void
        {
            if(event.keyCode == Keyboard.LEFT)
            {
                leftDown = false;
            }
            if(event.keyCode == Keyboard.RIGHT)
            {
                rightDown = false;
            }
        }
        
        public function moveChar(event:Event):void
        {
            if (leftDown)
            {
                this.x -= speed;
            }
            if (rightDown)
            {
                this.x += speed;
            }
            if (jump)
            {
                this.y -= velocity;
                velocity -= 1.5;
            }
        }
    }
}
  1. package
  2. {
  3.     import flash.ui.Keyboard;
  4.     import flash.events.*;
  5.     import flash.display.*;
  6.     public class Ball extends MovieClip
  7.     {     
  8.         public var leftDown:Boolean = false;
  9.         public var rightDown:Boolean = false;
  10.         public var jump:Boolean = false;
  11.         public var velocity:Number = 20;
  12.         public var falling:Boolean = true;
  13.         public var speed:int = 5;
  14.         public var onGround:Boolean = false;
  15.     
  16.         public function Ball()
  17.         {         
  18.             stage.addEventListener(KeyboardEvent.KEY_DOWN, downDetect);
  19.             stage.addEventListener(KeyboardEvent.KEY_UP, upDetect);
  20.             stage.addEventListener(Event.ENTER_FRAME, moveChar);
  21.         }
  22.         
  23.         public function downDetect(event:KeyboardEvent):void
  24.         {
  25.             if(event.keyCode == Keyboard.LEFT)
  26.             {
  27.                 leftDown = true;
  28.             }
  29.             if(event.keyCode == Keyboard.RIGHT)
  30.             {
  31.                 rightDown = true;
  32.             }
  33.             if(event.keyCode == Keyboard.UP && !falling)
  34.             {
  35.                 jump = true;
  36.             }
  37.         }
  38.         
  39.         
  40.         public function upDetect(event:KeyboardEvent):void
  41.         {
  42.             if(event.keyCode == Keyboard.LEFT)
  43.             {
  44.                 leftDown = false;
  45.             }
  46.             if(event.keyCode == Keyboard.RIGHT)
  47.             {
  48.                 rightDown = false;
  49.             }
  50.         }
  51.         
  52.         public function moveChar(event:Event):void
  53.         {
  54.             if (leftDown)
  55.             {
  56.                 this.x -= speed;
  57.             }
  58.             if (rightDown)
  59.             {
  60.                 this.x += speed;
  61.             }
  62.             if (jump)
  63.             {
  64.                 this.y -= velocity;
  65.                 velocity -= 1.5;
  66.             }
  67.         }
  68.     }
  69. }


Clase que contiene la Ground.as HitTest:
Código: [ Select ]
package
{
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.events.*;

public class Ground extends MovieClip
{
    private var _ball_mc:MovieClip;
    private var _findStageInstances:Boolean;
    
    public function Ground()
    {
        stage.addEventListener(Event.ENTER_FRAME, hitCheck);
        _findStageInstances = true;
    }
    
    public function hitCheck(event:Event):void
    {
        if (_findStageInstances)
        {
            _ball_mc = parent.getChildByName("ball_mc") as MovieClip;
            _findStageInstances = false;
        }

        if (_ball_mc.hitTestObject(this))
        {
            _ball_mc.jump = false;
            _ball_mc.falling = false;
            _ball_mc.onGround = true;
            _ball_mc.velocity = 20;
            _ball_mc.onGround = true;
            
            stage.addEventListener(KeyboardEvent.KEY_DOWN, downDetect);
            function downDetect(event:KeyboardEvent):void
            {
                if(event.keyCode == Keyboard.UP)
                {
                    stage.removeEventListener(Event.ENTER_FRAME, hitCheck);
                    _ball_mc.jump = true;
                }
            stage.addEventListener(Event.ENTER_FRAME, hitCheck);
            }
        }
        
        if (!_ball_mc.hitTestObject(this))
        {
            _ball_mc.onGround = false;
        }     
        
        if (!_ball_mc.hitTestObject(this) && !_ball_mc.jump && !_ball_mc.onGround)
        {
            _ball_mc.y += 7;
            _ball_mc.falling = true;
        }
        
        if (_ball_mc.onGround)
        {
            _ball_mc.y = this.y - this.height - 13;
        }
    }
}
}
  1. package
  2. {
  3. import flash.display.MovieClip;
  4. import flash.ui.Keyboard;
  5. import flash.events.*;
  6. public class Ground extends MovieClip
  7. {
  8.     private var _ball_mc:MovieClip;
  9.     private var _findStageInstances:Boolean;
  10.     
  11.     public function Ground()
  12.     {
  13.         stage.addEventListener(Event.ENTER_FRAME, hitCheck);
  14.         _findStageInstances = true;
  15.     }
  16.     
  17.     public function hitCheck(event:Event):void
  18.     {
  19.         if (_findStageInstances)
  20.         {
  21.             _ball_mc = parent.getChildByName("ball_mc") as MovieClip;
  22.             _findStageInstances = false;
  23.         }
  24.         if (_ball_mc.hitTestObject(this))
  25.         {
  26.             _ball_mc.jump = false;
  27.             _ball_mc.falling = false;
  28.             _ball_mc.onGround = true;
  29.             _ball_mc.velocity = 20;
  30.             _ball_mc.onGround = true;
  31.             
  32.             stage.addEventListener(KeyboardEvent.KEY_DOWN, downDetect);
  33.             function downDetect(event:KeyboardEvent):void
  34.             {
  35.                 if(event.keyCode == Keyboard.UP)
  36.                 {
  37.                     stage.removeEventListener(Event.ENTER_FRAME, hitCheck);
  38.                     _ball_mc.jump = true;
  39.                 }
  40.             stage.addEventListener(Event.ENTER_FRAME, hitCheck);
  41.             }
  42.         }
  43.         
  44.         if (!_ball_mc.hitTestObject(this))
  45.         {
  46.             _ball_mc.onGround = false;
  47.         }     
  48.         
  49.         if (!_ball_mc.hitTestObject(this) && !_ball_mc.jump && !_ball_mc.onGround)
  50.         {
  51.             _ball_mc.y += 7;
  52.             _ball_mc.falling = true;
  53.         }
  54.         
  55.         if (_ball_mc.onGround)
  56.         {
  57.             _ball_mc.y = this.y - this.height - 13;
  58.         }
  59.     }
  60. }
  61. }


Gracias!
  • Anonymous
  • Bot
  • No Avatar
  • Registrado: 25 Feb 2008
  • Mensajes: ?
  • Loc: Ozzuland
  • Status: Online

Nota Febrero 16th, 2009, 1:36 pm

  • ryrocks
  • Newbie
  • Newbie
  • No Avatar
  • Registrado: Feb 13, 2009
  • Mensajes: 6
  • Status: Offline

Nota Febrero 17th, 2009, 9:11 am

Chicos te jodan, Ive arreglado! (es broma!)

Yo estaba cerca antes, estaba excesivamente las cosas. Que necesitaba para eliminar el EventListener cuando el HitTest fue detectado porque había un cierto punto donde el pensamiento de los objetos flash se wernt golpear y golpear al mismo tiempo: - \ Por lo tanto, establecer la posición de la bola hasta la posición deseada, pero luego se trasladó hacia abajo 7px como si estuviera cayendo.
Luego vuelve a conectar el EventListener después de que el método HitTest verdad.

Que probablemente no tenía ningún sentido para usted! (difícil de explicar!)

Anywhoooo, heres el código de trabajo:

Código: [ Select ]
public function hitCheck(event:Event):void
    {
        if (_findStageInstances)
        {
            _ball_mc = parent.getChildByName("ball_mc") as MovieClip;
            _findStageInstances = false;
        }

        if (_ball_mc.hitTestObject(this))
        {
            _ball_mc.jump = false;
            _ball_mc.falling = false;
            _ball_mc.y = (this.y);
            _ball_mc.velocity = 17;        
            stage.removeEventListener(Event.ENTER_FRAME, hitCheck);
        }
        stage.addEventListener(Event.ENTER_FRAME, hitCheck);
        
        if (!_ball_mc.hitTestObject(this) && !_ball_mc.jump)
        {
            _ball_mc.y += 4;
            _ball_mc.falling = true;    
        }
    }
  1. public function hitCheck(event:Event):void
  2.     {
  3.         if (_findStageInstances)
  4.         {
  5.             _ball_mc = parent.getChildByName("ball_mc") as MovieClip;
  6.             _findStageInstances = false;
  7.         }
  8.         if (_ball_mc.hitTestObject(this))
  9.         {
  10.             _ball_mc.jump = false;
  11.             _ball_mc.falling = false;
  12.             _ball_mc.y = (this.y);
  13.             _ball_mc.velocity = 17;        
  14.             stage.removeEventListener(Event.ENTER_FRAME, hitCheck);
  15.         }
  16.         stage.addEventListener(Event.ENTER_FRAME, hitCheck);
  17.         
  18.         if (!_ball_mc.hitTestObject(this) && !_ball_mc.jump)
  19.         {
  20.             _ball_mc.y += 4;
  21.             _ball_mc.falling = true;    
  22.         }
  23.     }
  • graphixboy
  • Control + Z
  • Mastermind
  • Avatar de Usuario
  • Registrado: Jul 11, 2005
  • Mensajes: 1828
  • Loc: In the Great White North
  • Status: Offline

Nota Febrero 17th, 2009, 10:22 am

Me alegra que se fija. Yo sólo no había llegado hasta aquí abajo en la lista aún :-)
If at first you don't succeed F1... If that doesn't work try Google!
//// Designer, Developer & Teacher - Interactive, Motion and 3D \\\\
Portfolio at WhenImNotSleeping.com

Publicar Información

  • Total de mensajes en este tema: 3 mensajes
  • Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 57 invitados
  • No puede abrir nuevos temas en este Foro
  • No puede responder a temas en este Foro
  • No puede editar sus mensajes en este Foro
  • No puede borrar sus mensajes en este Foro
  • No puede enviar adjuntos en este Foro
 
 

© 2011 Unmelted, LLC. Ozzu® es una marca registrada de Unmelted, LLC