Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 09-04-2006
edusus edusus is offline
Miembro
 
Registrado: ene 2006
Posts: 47
Poder: 0
edusus Va por buen camino
mover image con teclado

Como puedo hacer para mover un control image con el teclado, usando las teclas de arriba, abajo, izquierda y derecha.
He probado con:
if key=vk_down then ...;, pero no funciona.
Gracias por adelantado.
Responder Con Cita
  #2  
Antiguo 09-04-2006
Avatar de Sotrono
Sotrono Sotrono is offline
Miembro
 
Registrado: abr 2004
Ubicación: Buenos Aires - Argentina
Posts: 396
Poder: 21
Sotrono Va por buen camino
Hola, de que componente estas usando el evento OnKeyDown u OnKeyUp??
Tenes activada la propiedad KeyPreview del Form??

Esto funciona:
Código Delphi [-]
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = vk_right then
    Image1.Left := Image1.Left + 20;
end;

Bytes...
Responder Con Cita
  #3  
Antiguo 09-04-2006
Avatar de seoane
[seoane] seoane is offline
Miembro Premium
 
Registrado: feb 2004
Ubicación: A Coruña, España
Posts: 3.717
Poder: 24
seoane Va por buen camino
Si estas programando algun tipo de juego y necesitas, ademas de los movimientos de arriba, abajo, izquierda y derecha, movimientos en diagonal puedes usar algo como esto:

Código Delphi [-]
  // Coloca esto dentro de un timer con un intervalo, por ejemplo, de 100 milisegundos
  if (GetKeyState(VK_LEFT) and $80) > 0 then
    Imagen.Left:= Imagen.Left - 10;
  if (GetKeyState(VK_RIGHT) and $80) > 0 then
    Imagen.Left:= Imagen.Left + 10;
  if (GetKeyState(VK_UP) and $80) > 0 then
    Imagen.Top:= Imagen.Top - 10;
  if (GetKeyState(VK_DOWN) and $80) > 0 then
    Imagen.Top:= Imagen.Top + 10;

Última edición por seoane fecha: 09-04-2006 a las 21:57:10.
Responder Con Cita
  #4  
Antiguo 09-04-2006
Avatar de dec
dec dec is offline
Moderador
 
Registrado: dic 2004
Ubicación: Alcobendas, Madrid, España
Posts: 13.107
Poder: 34
dec Tiene un aura espectaculardec Tiene un aura espectacular
Hola,

Me váis a perdonar la tontería, al Hilo de lo que habéis propuesto:

Código Delphi [-]
program PelotaLoca;

{$APPTYPE CONSOLE}

uses
  Forms,
  Windows,
  Classes,
  SysUtils,
  Controls,
  ExtCtrls,
  Graphics;

const
  MOVIMIENTO = 15; { Cantidad de movimiento }

var
  FForm  : TForm;
  FShape : TShape;

type
  TAuxiliar = class
  private
    FTimer: TTimer;
    procedure TimerTick(Sender: TObject);
    procedure KeyDownEvent(Sender: TObject;
               var Key: Word; Shift: TShiftState);
  public
    constructor Create;
    destructor Destroy; override;
  end;

{ TAuxiliar }

constructor TAuxiliar.Create;
begin
  FTimer := TTimer.Create(nil);
  FTimer.Enabled  := true;
  FTimer.Interval := 100;
  FTimer.OnTimer  := TimerTick;
end;

destructor TAuxiliar.Destroy;
begin
  FTimer.Free;
  inherited Destroy;
end;

procedure TAuxiliar.KeyDownEvent(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  with FShape do case Key of
    VK_UP:    Top  := Top  - MOVIMIENTO;
    VK_RIGHT: Left := Left + MOVIMIENTO;
    VK_DOWN:  Top  := Top  + MOVIMIENTO;
    VK_LEFT:  Left := Left - MOVIMIENTO;
  end;
end;

procedure TAuxiliar.TimerTick(Sender: TObject);
resourcestring
  rsCaption = 'Top: %d - Left: %d';
begin
  with FShape do
  begin
    FForm.Caption := Format(rsCaption, [Top, Left]);

    if (Left > FForm.Width)  then Left := 2;
    if (Top  > FForm.Height) then Top  := 2;

    if (Left < 0) then Left := (FForm.Width  - Width);
    if (Top  < 0) then Top  := (FForm.Height - Height);

    if (GetKeyState(VK_UP)    and $4000) > 0 then Top  := Top  - MOVIMIENTO;
    if (GetKeyState(VK_DOWN)  and $4000) > 0 then Top  := Top  + MOVIMIENTO;
    if (GetKeyState(VK_LEFT)  and $4000) > 0 then Left := Left - MOVIMIENTO;
    if (GetKeyState(VK_RIGHT) and $4000) > 0 then Left := Left + MOVIMIENTO;
  end;
end;

procedure Inicializar();
var
  FAuxilar: TAuxiliar;
begin
  FAuxilar := TAuxiliar.Create;
  FForm    := TForm.Create(nil);
  FShape   := TShape.Create(FForm);

  with FForm do
  begin
    Width       := 400;
    Height      := 300;
    Color       := clWhite;
    KeyPreview  := true;
    BorderStyle := bsDialog;
    Position    := poDesktopCenter;
    OnKeyDown   := FAuxilar.KeyDownEvent;
  end;

  with FShape do
  begin
    Width       := 20;
    Height      := 20;
    Parent      := FForm;
    Brush.Color := clRed;
    Pen.Color   := clWhite;
    Shape       := stCircle;
    Top         := MOVIMIENTO;
    Left        := MOVIMIENTO;
  end;

  try
    FForm.ShowModal;
  finally
    FForm.Free;
    FAuxilar.Free;
  end;
end;

begin { application }

  Inicializar();

end.
__________________
David Esperalta
www.decsoftutils.com
Responder Con Cita
  #5  
Antiguo 09-04-2006
edusus edusus is offline
Miembro
 
Registrado: ene 2006
Posts: 47
Poder: 0
edusus Va por buen camino
Funciona !!!

Muchas gracias por la explicación. Ha funcionado.
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Imagen de un Image a otro joHn je@N Gráficos 1 06-06-2005 10:06:00
image a qrimage soloriv Varios 0 25-05-2005 23:56:41
Imprimir Image acl_gandalf Impresión 2 01-02-2005 15:08:31
Impedir mover el form y redimensionar al mover barra tareas jealousy API de Windows 0 15-06-2004 12:58:05
Parpadeo en un Image esquerda21 Gráficos 5 07-11-2003 13:33:15


La franja horaria es GMT +2. Ahora son las 22:00:14.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi