Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET Game Programming (http://www.xtremevbtalk.com/-net-game-programming/)
-   -   Picturebox class for Trigers. (http://www.xtremevbtalk.com/-net-game-programming/327559-picturebox-class-trigers.html)

zacacp 04-08-2015 11:52 PM

Picturebox class for Trigers.
 
I dont know how to write an event from form1 into the class, i try with the "withevents" but doesnt work.

the code is simple and explain it better.

Code:

Public Class TRIGER
    Inherits PictureBox

    Public WithEvents CHARACTER As PictureBox = form1.CHARACTER

    Private Sub form1_CHARACTER_locationchanged(sender As Object, e As EventArgs) Handles CHARACTER.LocationChanged

        If form1.CHARACTER.Bounds.IntersectsWith(Me.Bounds) Then

        -SOMETHING

        End If
    End Sub


PlausiblyDamp 04-09-2015 03:28 AM

Not entirely sure what you are trying to do with the above code....

Are you creating your own picturebox that you want to be able to raise events that the form can handle or are you wanting your picturebox to handle events raised by the form?

passel 04-09-2015 05:51 AM

I'm think I know, although I wouldn't have thought to do collision testing this way.
I think rather than reference a control by name on the form, you should have either a method that can be called passing the control whose event you want to monitor, or pass the control to the constructor.
Here is a test case passing the picturebox to the constructor.
Code:

Public Class Form1
  Private myTrigger As TRIGER

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    myTrigger = New TRIGER(PictureBox1)
    Controls.Add(myTrigger)
    myTrigger.SetBounds(100, 100, 50, 50)
    myTrigger.BackColor = Color.Red

    PictureBox1.BackColor = Color.Green
  End Sub

  Private Sub PictureBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    'Allow dragging the picturebox around
    Static lpos As Point
    If e.Button = Windows.Forms.MouseButtons.Left Then
      PictureBox1.Location += New Size(e.Location - New Size(lpos))
    Else
      lpos = e.Location
    End If
  End Sub
End Class

Public Class TRIGER
  Inherits PictureBox
  Private WithEvents CHARACTER As PictureBox

  Public Sub New(p As PictureBox)
    CHARACTER = p
  End Sub

  Private Sub form1_CHARACTER_locationchanged(sender As Object, e As EventArgs) Handles CHARACTER.LocationChanged
    If CHARACTER.Bounds.IntersectsWith(Me.Bounds) Then
      BackColor = Color.Yellow
    Else
      BackColor = Color.Red
    End If
  End Sub
End Class

I'm still not sure of the efficiency of processing events this way, but since using a dictionary to hold multiple pictureboxes for collision testing against the trigger occurred to me when I first looked at your code, I went ahead and put together a test case showing that as well.
If I had multiple characters and multiple trigger points, all the multiple event processing would probably not be the way to go, but I couldn't say that for sure without testing. It is just an intuitive feeling, which isn't always correct in this age of event driven, object oriented processing.

In any case, here is my first idea of changing the code above to handle multiple characters against a single trigger object.
The example expects that three pictureboxes exist. Since the code already changed the background color of picturebox1, I set the background color of Picturebox3 to another color, and left picuturebox2 with its default background color.

This example reverses whose color gets changed. Instead of the trigger point changing color during a collision, the colliding pictureboxes color gets changed. This shows that each picturebox is being recognized independently from the others, and that all three pictureboxes location changed events are being processed by the TRIGER class.
Code:

Public Class Form1
  Private myTrigger As New TRIGER

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Controls.Add(myTrigger)
    myTrigger.SetBounds(100, 100, 50, 50)
    myTrigger.BackColor = Color.Red

    PictureBox1.BackColor = Color.Green

  'Add three pictureboxes that are monitored by the trigger and what color
  ' they will be set to when a collision occurs
    myTrigger.add(PictureBox1, Color.Yellow)
    myTrigger.add(PictureBox2, Color.Blue)
    myTrigger.add(PictureBox3, Color.Magenta)

  End Sub

 'Modified to handle dragging all three pictureboxes around
  Private Sub PictureBox_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles _
    PictureBox1.MouseMove, PictureBox2.MouseMove, PictureBox3.MouseMove
    'Allow dragging the picturebox around
    Dim pb As PictureBox = DirectCast(sender, PictureBox)
    Static lpos As Point
    If e.Button = Windows.Forms.MouseButtons.Left Then
      pb.Location += New Size(e.Location - New Size(lpos))
    Else
      lpos = e.Location
    End If
  End Sub
End Class

Public Class TRIGER
  Inherits PictureBox
  ' Private WithEvents CHARACTER As PictureBox
  Private Class objColors
    Public defaultColor As Color
    Public collisionColor As Color
  End Class

  Private Characters As New Dictionary(Of PictureBox, objColors)

  'Public Sub New(p As PictureBox)
  '  CHARACTER = p
  'End Sub
  Public Sub add(p As PictureBox, c As Color)
    If Characters.ContainsKey(p) Then
      Characters(p).collisionColor = c      'allow changing collision color
    Else
      Dim objColor As New objColors
      objColor.defaultColor = p.BackColor
      objColor.collisionColor = c
      Characters.Add(p, objColor)
      AddHandler p.LocationChanged, AddressOf Me.CHARACTER_locationchanged
    End If
  End Sub

  Private Sub CHARACTER_locationchanged(sender As Object, e As EventArgs)
    Dim p As PictureBox = DirectCast(sender, PictureBox)

    If p.Bounds.IntersectsWith(Me.Bounds) Then
      p.BackColor = Characters(p).collisionColor
    Else
      p.BackColor = Characters(p).defaultColor
    End If
  End Sub
End Class


zacacp 04-09-2015 07:37 PM

explaining
 
1 Attachment(s)
You can code "triggers" in one or two levels, but when you are dealing with so many forms(levels) that work turns to nightmare.
example=FORM1
I usually handle with all trigers in this form. Those "triggers" are pictureboxes, so i say that when the main character changes its location and intersects those "triggers" something will happen.
This is easy but you have to consider naming all the pictureboxes(triggers) in orden not to get confused while coding them. Finally you are happy because everything works Fine! but...then...you continue to the second form(level2) and do that same job over and over again.

i think in making a new picturebox class(template trigger) to solve this problem(stress) so i could add this controls and only code Once.

the picture ilustrates the concept i am trying to express in my limited English.


All times are GMT -6. The time now is 08:51 AM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.