Visual Basic Foro
Programación => Visual Basic 6 => Mensaje iniciado por: Bazooka en Julio 15, 2014, 08:04:25 pm
-
Hola amigos necesito crear en un Timer un desplazamiento de una imagen hacia un destino variable establecido por una cuadricula.
La cuadricula es una matriz de objetos ordenados según su INDEX de izquierda a derecha y de arriba hacia abajo (como muestra la imagen)
(http://www.misimagenesgratis.com.ar/images/grilla.jpg)
La idea es desplazar la Caja 'A' segun un valor aleatorio a uno de los objetos de la matrix en la direccion en que se encuentre !!
La verdad es que no me doy cuenta como hacerlo hasta aca llego! este es el codigo que comenze a hacer:
Private Sub Timer1_Timer()
Dim DestinoX As Long
Dim DestinoY As Long
Dim IncrementoX As Single
Dim IncrementoY As Single
DestinoX = Caja(actual).Top
DestinoY = Caja(actual).Left
'creo que en estas lineas esta el asunto pero no logre resolverlo
IncrementoX = 10 '????
IncrementoY = 20 '??
Image1.Left = Image1.Left + IncrementoY
Image1.Top = Image1.Top + IncrementoX
If Image1.Left >= DestinoY Then
Timer1 = False
End If
If Image1.Top >= DestinoX Then
Timer1 = False
End If
End Sub
-
1º No se ve la imagen
2º El codigo esta bien, pero el IncrementoY e IncrementoX deberian estar en Twips (15 twips = 1px, obtene esto por Screen.TwipsPerPixelX y Screen.TwipsPerPixelY)
3º la variable "actual" esta definida en otro lado, supongo que mientras el timer este activo NO deberia cambiar
4º La posicion de Caja(actual) define el limite maximo a donde se va a mover Image1, pero se detendra cuando dichas posiciones sean identicas. Considera en agregarle un "Image1.Top + Image1.Height >= DestinoX" para que "colisione" antes
-
Hola ahi compuse la imagen!!
Si la variable Actual esta definida globalmente y no cambia hasta que este proceso termine!
La idea es fundir este objeto justo cuando llegue a estar sobre el otro !!
-
Hola Bazooka, antes que nada te cuento que invertí el nombre en algunas variables ya que lo comun es tomar a Left = X e Top = Y
esto es lo que hice para que se mueva proporcionalmente a las distancia y lleguen al mismo tiempo ambos objetivos (X, Y).
Private Sub Timer1_Timer()
Dim DestinoX As Long
Dim DestinoY As Long
Dim IncrementoX As Single
Dim IncrementoY As Single
Dim lPercent As Long
Dim DistanciaX As Long
Dim DistanciaY As Long
Const nSaltos As Integer = 1000
DestinoX = Caja(Actual).Left
DestinoY = Caja(Actual).Top
DistanciaX = Abs(Image1.Left - DestinoX)
DistanciaY = Abs(Image1.Top - DestinoY)
If DistanciaX > DistanciaY Then
lPercent = (DistanciaY * 100 / DistanciaX)
IncrementoX = nSaltos '* lPercent / 100
IncrementoY = nSaltos * lPercent / 100
Else
lPercent = (DistanciaX * 100 / DistanciaY)
IncrementoX = nSaltos * lPercent / 100
IncrementoY = nSaltos
End If
If Image1.Left + IncrementoX < DestinoX Then
Image1.Left = Image1.Left + IncrementoX
Else
If Image1.Left - IncrementoX > DestinoX Then
Image1.Left = Image1.Left - IncrementoX
Else
Image1.Left = DestinoX
End If
End If
If Image1.Top + IncrementoY < DestinoY Then
Image1.Top = Image1.Top + IncrementoY
Else
If Image1.Top - IncrementoY > DestinoY Then
Image1.Top = Image1.Top - IncrementoY
Else
Image1.Top = DestinoY
End If
End If
If (Image1.Left = DestinoX) And (Image1.Top = DestinoY) Then
Timer1 = False
End If
End Sub
a mi gusto si pones el timer con un intervalo de 10 y a nSaltos = 1000 el efecto es lindo, pero vos modifica esos valores a tus gusto o como mejor se amolde al programa.
-
Hola Bazooka, antes que nada te cuento que invertí el nombre en algunas variables ya que lo comun es tomar a Left = X e Top = Y
esto es lo que hice para que se mueva proporcionalmente a las distancia y lleguen al mismo tiempo ambos objetivos (X, Y).
Private Sub Timer1_Timer()
Dim DestinoX As Long
Dim DestinoY As Long
Dim IncrementoX As Single
Dim IncrementoY As Single
Dim lPercent As Long
Dim DistanciaX As Long
Dim DistanciaY As Long
Const nSaltos As Integer = 1000
DestinoX = Caja(Actual).Left
DestinoY = Caja(Actual).Top
DistanciaX = Abs(Image1.Left - DestinoX)
DistanciaY = Abs(Image1.Top - DestinoY)
If DistanciaX > DistanciaY Then
lPercent = (DistanciaY * 100 / DistanciaX)
IncrementoX = nSaltos '* lPercent / 100
IncrementoY = nSaltos * lPercent / 100
Else
lPercent = (DistanciaX * 100 / DistanciaY)
IncrementoX = nSaltos * lPercent / 100
IncrementoY = nSaltos
End If
If Image1.Left + IncrementoX < DestinoX Then
Image1.Left = Image1.Left + IncrementoX
Else
If Image1.Left - IncrementoX > DestinoX Then
Image1.Left = Image1.Left - IncrementoX
Else
Image1.Left = DestinoX
End If
End If
If Image1.Top + IncrementoY < DestinoY Then
Image1.Top = Image1.Top + IncrementoY
Else
If Image1.Top - IncrementoY > DestinoY Then
Image1.Top = Image1.Top - IncrementoY
Else
Image1.Top = DestinoY
End If
End If
If (Image1.Left = DestinoX) And (Image1.Top = DestinoY) Then
Timer1 = False
End If
End Sub
a mi gusto si pones el timer con un intervalo de 10 y a nSaltos = 1000 el efecto es lindo, pero vos modifica esos valores a tus gusto o como mejor se amolde al programa.
Buenisimo Amigo Leandro!! ahora si que funciona Te molesto y te pido una cosa ...
Ya que la Caja A es mas grande que el destino(Caja(Actual) (casi el triple) podra ser que se vaya achicando hasta llegar al destino para que quede más convincente mi animación?
-
Algo rápido es hacer lo mismo pero cambias el Left por Width y el Top por el Height, en cuestiones de efecto, la llegada al destino y el ajuste de la imagen no van a ser exactamente al mismo tiempo, pero creo que es algo mínimo y safa.
Private Sub Timer1_Timer()
Dim DestinoX As Long, DestinoY As Long
Dim DestinoW As Long, DestinoH As Long
Dim IncrementoX As Single, IncrementoY As Single
Dim DistanciaX As Long, DistanciaY As Long
Dim lPercent As Long
Dim X As Long, Y As Long, W As Long, H As Long
Const nSaltos As Integer = 1000
Const nResize As Integer = 400
DestinoX = Caja(Actual).Left
DestinoY = Caja(Actual).Top
DestinoW = Caja(Actual).Width
DestinoH = Caja(Actual).Height
With Image1
DistanciaX = Abs(.Left - DestinoX)
DistanciaY = Abs(.Top - DestinoY)
If DistanciaX + DistanciaY > 0 Then
If DistanciaX > DistanciaY Then
lPercent = (DistanciaY * 100 / DistanciaX)
IncrementoX = nSaltos
IncrementoY = nSaltos * lPercent / 100
Else
lPercent = (DistanciaX * 100 / DistanciaY)
IncrementoX = nSaltos * lPercent / 100
IncrementoY = nSaltos
End If
If .Left + IncrementoX < DestinoX Then
X = .Left + IncrementoX
Else
If .Left - IncrementoX > DestinoX Then
X = .Left - IncrementoX
Else
X = DestinoX
End If
End If
If .Top + IncrementoY < DestinoY Then
Y = .Top + IncrementoY
Else
If .Top - IncrementoY > DestinoY Then
Y = .Top - IncrementoY
Else
Y = DestinoY
End If
End If
Else
X = DestinoX
Y = DestinoY
End If
DistanciaX = Abs(.Width - DestinoW)
DistanciaY = Abs(.Height - DestinoH)
If DistanciaX + DistanciaY > 0 Then
If DistanciaX > DistanciaY Then
lPercent = (DistanciaY * 100 / DistanciaX)
IncrementoX = nResize
IncrementoY = nResize * lPercent / 100
Else
lPercent = (DistanciaX * 100 / DistanciaY)
IncrementoX = nResize * lPercent / 100
IncrementoY = nResize
End If
If .Width + IncrementoX < DestinoW Then
W = .Width + IncrementoX
Else
If .Width - IncrementoX > DestinoW Then
W = .Width - IncrementoX
Else
W = DestinoW
End If
End If
If .Height + IncrementoY < DestinoH Then
H = .Height + IncrementoY
Else
If .Height - IncrementoY > DestinoH Then
H = .Height - IncrementoY
Else
H = DestinoH
End If
End If
Else
W = DestinoW
H = DestinoH
End If
.Move X, Y, W, H
If (.Left = DestinoX) And (.Top = DestinoY) And (.Width = DestinoW) And (.Height = DestinoH) Then
Timer1 = False
End If
End With
End Sub
-
SOS UN GENIO!! LEANDRO!!!!
Acá esta el videito de como va quedando pero aun faltan hacer unos ajustes!!
Y se ve un poco lento por la captura del video
[youtube]yRtY1-8cRDI&[/youtube]
-
Ahh, que bacan te quedo. Recomendacion: Cuando publiques un video usa el icono de youtube del foro:
(http://i.snag.gy/SfiCp.jpg)
y pega solo el codigo del video
(http://i.snag.gy/pTB2M.jpg)
para que te quede asi: (http://i.snag.gy/VHdKC.jpg)
[youtube]yRtY1-8cRDI[/youtube]
A lo importante:
Me parece que esta un poquito rápido el desplazamiento de la pelotita, desacelera un poquito, pero el rebote si te quedo de 100 puntos.