elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Security Series.XSS. [Cross Site Scripting]


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP)
| | | |-+  Programación Visual Basic (Moderadores: LeandroA, seba123neo)
| | | | |-+  Cambiar objeto a variable
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Cambiar objeto a variable  (Leído 1,951 veces)
HJZR4

Desconectado Desconectado

Mensajes: 101


C0N0C1M13NT0


Ver Perfil
Cambiar objeto a variable
« en: 13 Octubre 2007, 04:52 am »

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?


« Última modificación: 13 Octubre 2007, 04:58 am por HJZR4 » En línea

Para aprender solo hay una solución:
LeeR y Preguntar
Sancho.Mazorka


Desconectado Desconectado

Mensajes: 480


Gamer & Programador


Ver Perfil WWW
Re: Cambiar objeto a variable
« Respuesta #1 en: 13 Octubre 2007, 05:20 am »

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 ):
Código
  1. Private Sub Command1_Click()
  2. Dim Var As String
  3. Var.Name = Command1.Name
  4. 'Var ahora pasa a llamarse Command1
  5. End Sub

Sancho.Mazorka    :¬¬


En línea

Ganador Xeon Web Server ! ! !    Sancho.Mazorka :D
http://foro.elhacker.net/index.php/topic,171903.75.html


HJZR4

Desconectado Desconectado

Mensajes: 101


C0N0C1M13NT0


Ver Perfil
Re: Cambiar objeto a variable
« Respuesta #2 en: 13 Octubre 2007, 13:27 pm »

Código:
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?
En línea

Para aprender solo hay una solución:
LeeR y Preguntar
ranslsad


Desconectado Desconectado

Mensajes: 492


Dim Ranslsad as String * :P - Que Vicio!


Ver Perfil WWW
Re: Cambiar objeto a variable
« Respuesta #3 en: 13 Octubre 2007, 14:16 pm »

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:
Código:
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
En línea

HJZR4

Desconectado Desconectado

Mensajes: 101


C0N0C1M13NT0


Ver Perfil
Re: Cambiar objeto a variable
« Respuesta #4 en: 13 Octubre 2007, 22:37 pm »

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.

Código:
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.
En línea

Para aprender solo hay una solución:
LeeR y Preguntar
Spider-Net


Desconectado Desconectado

Mensajes: 1.165


Un gran poder conlleva una gran responsabilidad


Ver Perfil WWW
Re: Cambiar objeto a variable
« Respuesta #5 en: 13 Octubre 2007, 22:54 pm »

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.

Código
  1. Public m_System As New dx_System_Class
  2. 'numero de bloques es el total de los bloques que quieres crear
  3. Dim Bloque(1 to numero de bloques) As GFX_Rect
  4. Dim Char As GFX_Rect
  5.  
  6. Dim i as integer
  7.  
  8. Private Sub Form_Load()
  9.  
  10. For i=1 To Numero de bloques
  11.     Bloque(i).X = Shape(i).Left
  12.     Bloque(i).Y = Shape(i).Top
  13.     Bloque(i).Height = Shape(i).Height
  14.     Bloque(i).Width = Shape(i).Width
  15. Next i
  16.  
  17. End Sub
  18.  

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.
« Última modificación: 13 Octubre 2007, 22:56 pm por Spider-Net » En línea

ranslsad


Desconectado Desconectado

Mensajes: 492


Dim Ranslsad as String * :P - Que Vicio!


Ver Perfil WWW
Re: Cambiar objeto a variable
« Respuesta #6 en: 14 Octubre 2007, 02:28 am »

Pues si.. eso es ... tambien..
pero si quieres que cada bloque este identificado por una variable con indexs  usa el mio..
Código:
dim bloques( 0 to 99) as GFX_Rect
y tendrias 100 bloques..
los bloques irian asi

Código:
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
En línea

HJZR4

Desconectado Desconectado

Mensajes: 101


C0N0C1M13NT0


Ver Perfil
Re: Cambiar objeto a variable
« Respuesta #7 en: 14 Octubre 2007, 03:44 am »

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.
En línea

Para aprender solo hay una solución:
LeeR y Preguntar
HJZR4

Desconectado Desconectado

Mensajes: 101


C0N0C1M13NT0


Ver Perfil
Re: Cambiar objeto a variable
« Respuesta #8 en: 14 Octubre 2007, 03:51 am »

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_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


« Última modificación: 14 Octubre 2007, 03:59 am por HJZR4 » En línea

Para aprender solo hay una solución:
LeeR y Preguntar
HJZR4

Desconectado Desconectado

Mensajes: 101


C0N0C1M13NT0


Ver Perfil
Re: Cambiar objeto a variable
« Respuesta #9 en: 14 Octubre 2007, 04:37 am »

Solucionado... XDDDD el error era mas tonto de lo que creía...

Código:
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.
En línea

Para aprender solo hay una solución:
LeeR y Preguntar
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
como cambiar valor de otro objeto?
Java
greenselves 1 4,023 Último mensaje 22 Marzo 2011, 19:00 pm
por Debci
[Ruby] Modificar variable de un objeto dentro de una clase
Scripting
Eleкtro 2 3,026 Último mensaje 23 Febrero 2012, 10:52 am
por Eleкtro
Referencia de variable y objeto tras ingresarse como argumento
Java
Fabi0lo 2 2,485 Último mensaje 3 Abril 2012, 15:32 pm
por Fabi0lo
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines