Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Collision

Collision

Scheduled Pinned Locked Moved C#
learning
21 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Martin 0

    Hello, You have to be carefull with the GDI objects here (memory leak)! If you want to hold the instance of your pens for further use, you have to hold the m as local members of your class. Same to Rectangles. But you have to dispose (free the resourcess) them in the dispose method of your class (Form). System Drawing.Rectangle rectangle1 = System Drawing.Rectangle.Empty; System Drawing.Rectangle rectangle2 = System Drawing.Rectangle.Empty; System.Drawing.Pen penBlack = new Pen(Color.Black, 3); System.Drawing.Pen penRed = new Pen(Color.Red, 3); //You could also use only one pen and Change the Color like you need! private void Form1_Paint(object sender, PaintEventArgs e) { Graphics actGraphics = e.Graphics; rectangle1 = new System Drawing.Rectangle(15, 15, 200, 150); rectangle2 = new System Drawing.Rectangle(35, 35, 200, 150); actGraphics.DrawRectangle(penBlack , rectangle1); actGraphics.DrawRectangle(penRed , rectangle2); } protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } if(penBlack !=null) penBlack.Dispose(); if(penRed !=null) penRed.Dispose(); } base.Dispose( disposing ); } If you not want to hold the instances (not suggested in this case), you would have to dispose the pens write after usage in your Paint event. A using block will do that automaticaly. private void Form1_Paint(object sender, PaintEventArgs e) { Graphics actGraphics = e.Graphics; using(System.Drawing.Pen penBlack = new Pen(Color.Black, 3)) using(System.Drawing.Pen penRed = new Pen(Color.Red, 3)) { rectangle1 = new System Drawing.Rectangle(15, 15, 200, 150); rectangle2 = new System Drawing.Rectangle(35, 35, 200, 150); actGraphics.DrawRectangle(penBlack , rectangle1); actGraphics.DrawRectangle(penRed , rectangle2); } } Hope it helps!

    All the best, Martin

    M Offline
    M Offline
    MasterSharp
    wrote on last edited by
    #21

    That just helped a lot, and there's SIMPLE collision detection! All I fond was 50 lines of code that was... extremely confusing and hard, but thatnks again to everyone who replied... Time to continue reading.

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups