Se puede convertir un objeto a una variable?, es decir:
Yo tengo Shape1 y la quiero convertir a shape (siendo "shape" el nombre de la variable). ¿Se podría?
No entendi bien, pero creo que queres convertir el nombre de algo en una variable...Creo que yo tambien pregunte esto una vez y me dijeron que no!
Ej ( es pseudocodigo de visual basic ):
Private Sub Command1_Click()
Dim Var As String
Var.Name = Command1.Name
'Var ahora pasa a llamarse Command1
End Sub
Sancho.Mazorka :¬¬
Option Explicit
Public m_System As New dx_System_Class
'Variable para Arrays
Public i As Integer
Public n As Integer
'Variables para el juego
'Muros
Dim M(0 To 8) As GFX_Rect
Dim Wall(0 To 8) As Shape
'Char
Dim c As GFX_Rect
'Dimensiones de los muros y del char
Const MuroWidth = 255
Const MuroHeight = 255
Const CharWidth = 255
Const CharHeight = 255
'Movimiento de Char
Const MovChar = 240
Private Sub CargarNivel()
For i = 0 To 8
Set Wall(i) = Muro(i)
Next i
For i = 0 To 8
M(i).X = Muro(i).Left
M(i).Y = Muro(i).Top
M(i).Width = Muro(i).Width
M(i).Height = Muro(i).Height
Next i
c.X = Char.Left
c.Y = Char.Top
c.Width = Char.Width
c.Height = Char.Height
End Sub
Private Sub Form_Load()
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyLeft
Char.Left = Char.Left - MovChar
c.X = Char.Left
c.Y = Char.Top
c.Width = Char.Width
c.Height = Char.Height
For i = 0 To 8
'Esto comprueba si hay colision
LabelColision.Caption = m_System.MATH_IntersectRect(M(i), c)
Next i
'Si hay colision, el char "no se mueve"
If LabelColision.Caption = "Verdadero" Then
Char.Left = Char.Left + MovChar
Label1.Caption = "Sorry, not possible."
End If
Case vbKeyRight
Char.Left = Char.Left + MovChar
c.X = Char.Left
c.Y = Char.Top
c.Width = Char.Width
c.Height = Char.Height
For i = 0 To 8
LabelColision.Caption = m_System.MATH_IntersectRect(M(i), c)
Next i
If LabelColision.Caption = "Verdadero" Then
Char.Left = Char.Left - MovChar
Label1.Caption = "Sorry, not possible."
End If
Case vbKeyDown
Char.Top = Char.Top + MovChar
c.X = Char.Left
c.Y = Char.Top
c.Width = Char.Width
c.Height = Char.Height
For i = 0 To 8
LabelColision.Caption = m_System.MATH_IntersectRect(M(i), c)
Next i
If LabelColision.Caption = "Verdadero" Then
Char.Top = Char.Top - MovChar
Label1.Caption = "Sorry, not possible."
End If
Case vbKeyUp
Char.Top = Char.Top - MovChar
c.X = Char.Left
c.Y = Char.Top
c.Width = Char.Width
c.Height = Char.Height
For i = 0 To 8
LabelColision.Caption = m_System.MATH_IntersectRect(M(i), c)
Next i
If LabelColision.Caption = "Verdadero" Then
Char.Top = Char.Top + MovChar
Label1.Caption = "Sorry, not possible."
End If
Case Else
Exit Sub
End Select
End Sub
Pero no va. Si hay por ejemplo 50 muros, el código sería enorme, por eso hago Muro(i). Se puede hacer de alguna manera?
Lo que quieres es una variable con indexs?
Si es asi...
se declara asi:
Dim ema(1 To 100) As String
..
un ejemplito para que veas mejor:
Dim ema(1 To 100) As String
ema(1) = "ema1"
ema(2) = "ema2"
MsgBox ema(1) & "|" & ema(2)
Espero que te haya servido ;)
Salu
Ranslsad
Mmmm... Lo expongo de otra manera.
Estoy usando una librería llamada dx_lib32.dll (diseñada para videojuegos). Para definir las colisiones, es necesario colocar un objeto en el form(diseño) y una variable en el código, indicando en la variable el tamaño y posición de su objeto. Pongo el code para que sea mas fácil de entender.
Option Explicit
Public m_System As New dx_System_Class
Dim Bloque1 As GFX_Rect
Dim Bloque2 As GFX_Rect
Dim Bloque3 As GFX_Rect
Dim Char As GFX_Rect
Private Sub Form_Load()
Bloque1.X = Shape1.Left
Bloque1.Y = Shape1.Top
Bloque1.Height = Shape1.Height
Bloque1.Width = Shape1.Width
Bloque2.X = Shape2.Left
Bloque2.Y = Shape2.Top
Bloque2.Height = Shape2.Height
Bloque2.Width = Shape2.Width
Bloque3.X = Shape3.Left
Bloque3.Y = Shape3.Top
Bloque3.Height = Shape3.Height
Bloque3.Width = Shape3.Width
End Sub
'...
Este código sólo detecta 3 bloques que impiden el paso, ahora imaginaros que quereis 100 bloques. Escribir eso sería un castigo.
Pues preguntaba que si habria alguna manera de ahorrar código.
Gracias.
PD: Si no está claro, decidlo.
Yo no estoy muy seguro de esto.. ¿¿pero podrías crearlo a lo mejor con un bucle?? Lo digo porque lo he estado pensando y se me vino esa idea pero no tengo ni idea de si se puede... yo pensé algo así... siendo Shape y Bloque arrays.
Public m_System As New dx_System_Class
'numero de bloques es el total de los bloques que quieres crear
Dim Bloque(1 to numero de bloques) As GFX_Rect
Dim Char As GFX_Rect
Dim i as integer
Private Sub Form_Load()
For i=1 To Numero de bloques
Bloque(i).X = Shape(i).Left
Bloque(i).Y = Shape(i).Top
Bloque(i).Height = Shape(i).Height
Bloque(i).Width = Shape(i).Width
Next i
End Sub
Es una teoría eh, yo no sé si eso se podrá hacer o no porque en mi vida he programado un juego en Visual Basic pero viendo el problema eso es lo que se me ocurrió.
Saludos.
Pues si.. eso es ... tambien..
pero si quieres que cada bloque este identificado por una variable con indexs usa el mio..
dim bloques( 0 to 99) as GFX_Rect
y tendrias 100 bloques..
los bloques irian asi
dim bloques( 0 to 99) as GFX_Rect
dim i as integer
for i = 0 to 99
Bloque(i).X = Shape1.Left
Bloque(i).Y = Shape1.Top
Bloque(i).Height = Shape1.Height
Bloque(i).Width = Shape1.Width
next i
y.. yasta!
Facil ;)
Salu2
Ranslsad
Eso es exactamente lo que quería pero he probado de mil maneras y no lo he conseguido, voy a probar haber, como tu has dicho...
Te aviso.
Modificado: He solucionado, era un error mio, xDDDDD
Ahora me salta error en tiempo de ejecución:
"el subindice está fuera del intervalo"
Eso me salta, al comprobar que esta colisionando... :S
Código completo:
Public m_System As New dx_System_Class
Dim Bloque(1 To 10) As GFX_Rect
Dim c As GFX_Rect
Dim i As Integer
Private Sub Form_Load()
For i = 1 To 10
Bloque(i).X = Shape(i).Left
Bloque(i).Y = Shape(i).Top
Bloque(i).Height = Shape(i).Height
Bloque(i).Width = Shape(i).Width
Next i
c.X = Char.Left
c.Y = Char.Top
c.Width = Char.Width
c.Height = Char.Height
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyLeft
Char.Left = Char.Left - 160
c.X = Char.Left
c.Y = Char.Top
c.Width = Char.Width
c.Height = Char.Height
LabelColision.Caption = m_System.MATH_IntersectRect(Bloque(i), c)
'Si hay colision, el char "no se mueve"
If LabelColision.Caption = "Verdadero" Then
Char.Left = Char.Left + 160
Label1.Caption = "Colision"
End If
Case vbKeyRight
Char.Left = Char.Left + 160
c.X = Char.Left
c.Y = Char.Top
c.Width = Char.Width
c.Height = Char.Height
LabelColision.Caption = m_System.MATH_IntersectRect(Bloque(i), c)
If LabelColision.Caption = "Verdadero" Then
Char.Left = Char.Left - 160
Label1.Caption = "Colision"
End If
Case vbKeyDown
Char.Top = Char.Top + 160
c.X = Char.Left
c.Y = Char.Top
c.Width = Char.Width
c.Height = Char.Height
LabelColision.Caption = m_System.MATH_IntersectRect(Bloque(i), c)
If LabelColision.Caption = "Verdadero" Then
Char.Top = Char.Top - 160
Label1.Caption = "Colision"
End If
Case vbKeyUp
Char.Top = Char.Top - 160
c.X = Char.Left
c.Y = Char.Top
c.Width = Char.Width
c.Height = Char.Height
LabelColision.Caption = m_System.MATH_IntersectRect(Bloque(i), c)
If LabelColision.Caption = "Verdadero" Then
Char.Top = Char.Top - 160
Label1.Caption = "Colision"
End If
Case Else
Exit Sub
End Select
End Sub
Solucionado... XDDDD el error era mas tonto de lo que creía...
Modificado: He solucionado, era un error mio, xDDDDD
Ahora me salta error en tiempo de ejecución:
"el subindice está fuera del intervalo"
Eso me salta, al comprobar que esta colisionando... :S
Código completo:
Código:
Public m_System As New dx_System_Class
Dim Bloque(1 To 10) As GFX_Rect
Dim c As GFX_Rect
Dim i As Integer
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyLeft
Char.Left = Char.Left - 160
c.X = Char.Left
c.Y = Char.Top
c.Width = Char.Width
c.Height = Char.Height
'!!!!!!!!!!!!!!!AQUI TENIA QUE PONER EL RANGO DE NUMEROS
LabelColision.Caption = m_System.MATH_IntersectRect(Bloque(i), c)
'Si hay colision, el char "no se mueve"
If LabelColision.Caption = "Verdadero" Then
Char.Left = Char.Left + 160
Label1.Caption = "Colision"
End If
'!!!!!!!!!!!!!!!!!AQUI EL FINAL (Next i)
Case vbKeyRight
Char.Left = Char.Left + 160
c.X = Char.Left
c.Y = Char.Top
c.Width = Char.Width
c.Height = Char.Height
LabelColision.Caption = m_System.MATH_IntersectRect(Bloque(i), c)
If LabelColision.Caption = "Verdadero" Then
Char.Left = Char.Left - 160
Label1.Caption = "Colision"
End If
Case vbKeyDown
Char.Top = Char.Top + 160
c.X = Char.Left
c.Y = Char.Top
c.Width = Char.Width
c.Height = Char.Height
LabelColision.Caption = m_System.MATH_IntersectRect(Bloque(i), c)
If LabelColision.Caption = "Verdadero" Then
Char.Top = Char.Top - 160
Label1.Caption = "Colision"
End If
Case vbKeyUp
Char.Top = Char.Top - 160
c.X = Char.Left
c.Y = Char.Top
c.Width = Char.Width
c.Height = Char.Height
LabelColision.Caption = m_System.MATH_IntersectRect(Bloque(i), c)
If LabelColision.Caption = "Verdadero" Then
Char.Top = Char.Top - 160
Label1.Caption = "Colision"
End If
PD: He dejado el codigo anterior, por si sirve de algo, para que se entienda todo.
Muxisisimas gracias a todos, aunque en especial a ranslsad, que a sido el que me ha hecho dar vueltas a la cabeza.. xDDD Gracias :p
Tema zanjado.